LeetCode 字符串中的第一個唯一字符

# 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章