【力扣題】題目描述:
【Python3】代碼:
1、解題思路:遍歷字符串,依次判斷元素在字符串中的個數(shù)是否為1,第一個為1的返回該元素的索引號,若整個字符串都沒有個數(shù)為1的,則返回-1。
知識點:enumerate(序列):返回可迭代的序列中所有索引號和對應(yīng)元素,元組形式 (索引,?元素)。
? ? ? ? ? ? ? 序列.count(...):統(tǒng)計某元素在序列中的個數(shù)。
class Solution:
def firstUniqChar(self, s: str) -> int:
for i,x in enumerate(s):
if s.count(x) == 1:
return i
return -1
2、解題思路:使用計數(shù)器統(tǒng)計出字符串中所有元素及其個數(shù),再次遍歷字符串,依次判斷元素在計數(shù)器中的個數(shù)是否為1,若是,返回索引,若整個字符串都沒有個數(shù)為1的,則返回-1。
知識點:collections.Counter(...):計數(shù)器,字典子類。統(tǒng)計序列中元素及其出現(xiàn)個數(shù)。
? ? ? ? ? ? ? 字典[鍵]:獲取字典中鍵對應(yīng)的值?;蛐薷逆I對應(yīng)的值:字典[鍵]=值。
class Solution:
def firstUniqChar(self, s: str) -> int:
import collections
adict = collections.Counter(s)
for i,x in enumerate(s):
if adict[x] == 1:
return i
return -1
3、(1)解題思路:使用字典統(tǒng)計出字符串中所有元素及其第一次出現(xiàn)的索引,若重復(fù)出現(xiàn),則該元素在字典中對應(yīng)的值為-1;再次遍歷字典的所有值,在不是-1的值中找出最小的索引號,若沒有,返回-1。
知識點:dict():創(chuàng)建空字典。即{ }。
? ? ? ? ? ? ? len(序列):獲取序列的長度。
? ? ? ? ? ? ? 字典.values():返回可迭代的字典中的所有值。
class Solution:
def firstUniqChar(self, s: str) -> int:
adict = dict()
n = len(s)
for i,x in enumerate(s):
if x in adict:
adict[x] = -1
else:
adict[x] = i
first = n
for i in adict.values():
if i != -1 and i < first:
first = i
if first == n:
return -1
return first
?(2)解題思路:使用字典統(tǒng)計出字符串中所有元素及其第一次出現(xiàn)的索引,若重復(fù)出現(xiàn),則該元素在字典中對應(yīng)的值為字符串總長度加索引(方便后面獲取最小值);再次遍歷字典的所有值,找出最小的索引號。
知識點:min(...):獲取最小值。
class Solution:
def firstUniqChar(self, s: str) -> int:
adict = dict()
n = len(s)
for i,x in enumerate(s):
if x in adict:
adict[x] = i + n
else:
adict[x] = i
result = min(adict.values())
return -1 if result > n else result
4、解題思路:使用字典統(tǒng)計出字符串中所有元素及其第一次出現(xiàn)的索引,若重復(fù)出現(xiàn),則該元素在字典中對應(yīng)的值為-1;使用隊列依次以元組形式記錄元素及其第一次出現(xiàn)的索引號,若是重復(fù)的元素從隊列移除。最終隊列為空,則返回-1,否則返回隊列第一個元素中記錄的索引號。
知識點:collections.deque():雙端隊列。左端進(jìn),右端出。也可以右端進(jìn),左端出。
? ? ? ? ? ? ??隊列.append():從隊尾(右端)添加一個元素。
? ? ? ? ? ? ? 隊列.popleft():從隊頭(左端)移除一個元素,并返回該元素。
class Solution:
def firstUniqChar(self, s: str) -> int:
import collections
adict = dict()
n = len(s)
q = collections.deque()
for i,x in enumerate(s):
if x not in adict:
adict[x] = i
q.append((s[i],i))
else:
adict[x] = -1
while q and adict[q[0][0]] == -1:
q.popleft()
return -1 if not q else q[0][-1]
5、解題思路:從字符串中依次篩選出所有出現(xiàn)次數(shù)為1的元素,列表形式記錄,若空列表,則沒有不重復(fù)的元素,返回-1,否則返回列表中第一個元素在字符串中對應(yīng)的索引號?!驹摲椒ǔ鰰r間限制】
知識點:filter(predict,?可迭代對象):返回一個迭代器。篩選出所有符合條件的元素。
? ? ? ? ? ? ? lambda:匿名函數(shù)。
??????????????序列.index(...):獲取元素在序列中的索引號。
? ? ? ? ? ? ? 序列[索引]:獲取序列中索引號對應(yīng)的元素。
class Solution:
def firstUniqChar(self, s: str) -> int:
# 超出時間限制
alist = list(filter(lambda x:s.count(x)==1,s))
return -1 if len(alist)==0 else s.index(alist[0])
改進(jìn):遍歷字符串,依次判斷元素出現(xiàn)的次數(shù)是否為1,從字符串中過濾掉不滿足條件的元素,獲取第一個滿足條件的元素及之后所有元素,列表形式記錄,若空列表,則返回-1,否則返回列表中第一個元素在字符串中對應(yīng)的索引號。文章來源:http://www.zghlxwxcb.cn/news/detail-799963.html
知識點:itertools.dropwhile(predict,?可迭代對象):返回一個迭代器。依次遍歷可迭代對象,不滿足條件的內(nèi)容去除,第一個滿足條件的內(nèi)容及其之后所有內(nèi)容全部返回。文章來源地址http://www.zghlxwxcb.cn/news/detail-799963.html
class Solution:
def firstUniqChar(self, s: str) -> int:
import itertools
alist = list(itertools.dropwhile(lambda x:s.count(x)!=1, s))
return -1 if len(alist)==0 else s.index(alist[0])
到了這里,關(guān)于【Python3】【力扣題】387. 字符串中的第一個唯一字符的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!