LeetCode-剑指 Offer 50-第一个只出现一次的字符

    科技2022-08-16  103

    剑指 Offer 50. 第一个只出现一次的字符

    说明

    在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。

    示例

    s = "abaccdeff" 返回 "b" s = "" 返回 " "

    题解思路

    使用哈希表 遍历s,如果哈希表中存在,则值变为False,如果不存在,添加值为True 再次遍历s,如果哈希表中对应值为true,返回该值

    代码实现

    class Solution: def firstUniqChar(self, s: str) -> str: dic = dict() for i in s: if i in dic: dic[i] = False else: dic[i] = True for j in s: if dic[j]: return j return " "
    Processed: 0.029, SQL: 9