在一个长为
字符串中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
数据范围:
,且字符串只有字母组成。
要求:空间复杂度
,时间复杂度
class Solution: def FirstNotRepeatingChar(self, s): # write code here if not s: return -1 for i in range(len(s)): if s[i] not in s[i+1:] and s[i] not in s[:i]: return i return -1
1、整体思路的时间复杂度为O(n)。采用字典的思路。
2、循环遍历输入的字符串s
(1)当字符不存在于字典时,将字符作为键添加,同时赋值为1。
(2)当字符存在于字典时,将其值+1。
3、要求字典在添加的时候是,有序添加。(这里有个坑:python3.8默认是有序添加;但是python2.7会根据键,将字典中的键值对自动排序。所以需要引入collections中的OrderedDict)
from collections import OrderedDict d = OrderedDict()
4、因为字典中的键值对是有序添加,所以我们只需要在字典里面找到,第一个值为1的键,它就是我们需要的字符。
5、返回该字符在s字符串中的位置。
# -*- coding:utf-8 -*- from collections import OrderedDict class Solution: def FirstNotRepeatingChar(self, s): # write code here if len(s)<=0 or len(s)>10000: return -1 result = OrderedDict() for i in s: if i not in result.keys(): result.setdefault(i,1) else: result[i]+=1 new_key=list(result.keys()) new_value=list(result.values()) tempStr=None for key,value in zip(new_key,new_value): if value==1: tempStr = key break if tempStr: return s.index(key) else: return -1
def FirstNoteRepeatingChar(s):
dict = {}
res = None
for item in s:
dict[item] = dict.get(item,0)+1
for item in dict:
if dict[item]==1:
res = item
break
#return item
for i,item in enumerate(s):
if item==res:
return i
return -1
s = 'NXWtnzyoHoBhUJaPauJaAitLWNMlkKwDYbbigdMMaYfkVPhGZcrEwp'
FirstNoteRepeatingChar(s)
用了字典,在自己的ide编译,结果都没有问题,牛客上的结果就不一样,好奇怪,搞不明白了
class Solution:
def FirstNotRepeatingChar(self, s):
if not s:
return -1
# 新建一个字典
dic = {}
for item in s:
# 如果字符不存在字典的keys中,就把其和对应的索引添加至字典的key-value
if not dic.has_key(item):
dic[item] = s.index(item)
# 如果存在,就把当前字符key对应的value,设置为一个其它独特的值
else:
dic[item] = 10001 # 这里是由于题目说了字符串最长10000
# 然后返回字典中value的最小值就可以了
return min(dic.values()) if min(dic.values())!=10001 else -1
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
dict={}
res = -1
for i in s:
if i not in dict:
dict[i]=1
else:
dict[i]+=1
for i in range(len(s)):
if dict[s[i]]==1:
res = i
break
return res class Solution:
def FirstNotRepeatingChar(self, s):
slen = len(s)
if slen == 0:
return -1
if slen == 1:
return 0
ret = dict()
for i in s:
if i not in ret:
ret[i] = 1
else:
ret[i] += 1
for i in ret.items():
if i[1] == 1:
return s.index(i[0])
return -1 测试用例:google
期望输出:4
你的输出:5
# -*- coding:utf-8 -*-
"""
空间换时间
"""
class Solution:
def FirstNotRepeatingChar(self, s):
"""
Time: O(n)
Space: O(1)
:param s:
:return:
"""
d = {}
for element in s:
d[element] = d.get(element,0) + 1
for i in range(len(s)):
if d[s[i]] == 1:
return i
return -1
s = Solution()
ans = s.FirstNotRepeatingChar(" ")
print(ans) # -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.list1={}
def FirstNotRepeatingChar(self, s):
(936)# write code here
if s=='':
return -1
for i in range(len(s)):
if s[i] not in self.list1:
self.list1[s[i]]=[i,1]
else:
self.list1[s[i]][1]+=1
for key,value in self.list1.items():
if value[1]>1:
self.list1.pop(key)
a=self.list1.values()
a.sort()
return a[0][0] 这个题采用的是字典的方法,记录每个字符出现的次数,等循环完一遍后,再将其中出现次数多余一个的字符从字典中去除中去,然后返回第一个只出现一次字符的位置就可以了