题目:在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写)
比较简单,用个dict,循环遍历即可。
代码:
class Solution:
def func(self , s):
if len(s) == 0:
return None
save = dict()
for i in range(len(s)):
if s[i] not in save.keys():
save[s[i]] = i
else:
save.pop(s[i])
res = list(save.keys())[list(save.values()).index(min(list(save.values())))]
print(res)
return save
a = 'abbceeffgg'
a = 'leetcode'
a = 'loveleetcode'
s = Solution()
print(s.func(a))