利用python中的字典来记录字符出现次数并找到第一次只出现一次的字符。2*O(n)
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
alpha_cnt={}
for i in s:
if i not in alpha_cnt.keys():
alpha_cnt[i]=1
elif i in alpha_cnt.keys():
alpha_cnt[i]+=1
idx=0
for j in s:
if alpha_cnt[j]==1:
return idx
idx+=1
return -1
# write code here 


京公网安备 11010502036488号