注意python3直接字典遍历是有序的,而python2需要用有序字典
from collections import OrderedDict
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
hashtable = OrderedDict()
for i in s:
if i in hashtable:
hashtable[i] += 1
else:
hashtable[i] = 1
for j in hashtable:
if hashtable[j] == 1:
return s.index(j)
return -1
京公网安备 11010502036488号