題目描述:
在一個字符串(0<=字符串長度<=10000,全部由字母組成)中找到第一個只出現一次的字符,並返回它的位置, 如果沒有則返回 -1(需要區分大小寫).
思路:
開字典就可以了。
代碼:
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if s == "":
return -1
l = list(s)
d = {}
for i in range(len(l)):
if not d.has_key(l[i]):
d[l[i]] = i
else:
d[l[i]] = len(l) + 1
tup_list = sorted(d.items(), key=lambda item: item[1])
return tup_list[0][1]