面试题50. 第一个只出现一次的字符

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

示例:

s = "abaccdeff"
返回 "b"

s = "" 
返回 " "

可以活用python字典。

class Solution:
    def firstUniqChar(self, s: str) -> str:
        numdic = dict()
        for c in s:
            if c in numdic:
                numdic[c] += 1
            else:
                numdic[c] = 1
        
        for i in numdic:
            if numdic[i] == 1:
                return i
        return " "
发布了385 篇原创文章 · 获赞 210 · 访问量 52万+

猜你喜欢

转载自blog.csdn.net/qq_32146369/article/details/104501325