题目:
方法:
利用collections.OrderedDict库创建有序字典
遍历字符串,字母为k,下标为value放入字典中
遍历字典,value长度为1则返回
最后如果未找到不重复的字符,则返回-1
class Solution:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
dict1 = collections.OrderedDict()
for i in range(len(s)):
if s[i] in dict1:
dict1[s[i]].append(i)
else:
dict1[s[i]] = [i]
for v in dict1:
if len(dict1[v]) == 1:
return dict1[v][0]
return -1