一.找出第一个出现一次字符的位置
enumerate:将可遍历的数据对象(列表、数组、字符串)组合为索引序列,同时返回数据和下标
def FirstNotRepeatingChar(self, s):
# write code here
if s=='':
return -1
for index,val in enumerate(s):
if s.count(val)==1:
return index
return -1
二.找出第一个出现一次字符
哈希表
def firstUniqChar(self, s: str) -> str:
if s=='':
return ' '
dic={}
for i in s:
if i in dic:
dic[i]+=1
else:
dic[i]=1
for k in dic:
if dic[k]==1:
return k
return ' '
list.count():统计某个元素在列表中出现的次数
def firstUniqChar(self, s: str) -> str:
if s=='':
return ' '
for i in s:
if s.count(i)==1:
return i
return ' '