# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
#先使用字典存储每一个字母出现的次数以及第一次出现的位置
#用一个列表按出现顺序存储第一次出现的字母
#然后对列表中的字母挨个进行判断,若出现次数<=1,则返回字典中对应的位置
#空间复杂度O(n) 时间复杂度O(n)
if not s or len(list(s))<1:
return -1
dir={}
s=list(s)
first=[]
for i in range(len(s)):
if not dir.__contains__(s[i]):
dir[s[i]]=[1,i]
first.append(s[i])
else:
dir[s[i]][0]+=1
for i in first:
if dir[i][0]<=1:
return dir[i][1]
return -1
剑指offer:python——第一个只出现一次的字符
猜你喜欢
转载自blog.csdn.net/oh_lala/article/details/88427048
今日推荐
周排行