# LeetCode 字符串中的第一个唯一字符
# HashMap 用字典去模拟
class Solution(object):
def firstUniqChar(self, s: str) -> int:
dic = {}
# 记录字符出现次数
for c in s:
if c in dic:
dic[c] = dic[c] + 1
else:
dic[c] = 1
for k, v in dic.items():
if v == 1:
return s.index(k)
return -1
LeetCode 字符串中的第一个唯一字符
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章