运行时间:42ms
占用内存:5856k
用counter字典很简单
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
from collections import Counter
cs = Counter(s)
res = []
for i in cs:
if cs[i]==1:
res.append(s.index(i))
if res:
return min(res)
else:
return -1
运行时间:39ms
占用内存:5612k
更妙,直接遍历数组不要看字典
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
from collections import Counter
cs = Counter(s)
res = []
for i in range(len(s)):
if cs[s[i]]==1:
return i
return -1