面试题50. 第一个只出现一次的字符
2020-03-26 本文已影响0人
人一己千
题目
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。
示例:
s = "abaccdeff"
返回 "b"
s = ""
返回 " "
限制:
0 <= s 的长度 <= 50000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
哈希表过一遍统计数量,然后再过一遍找数量为1的。
代码
class Solution:
def firstUniqChar(self, s: str) -> str:
d = {}
for c in s:
d[c] = d[c]+ 1 if c in d else 1
for c in d:
if d[c] == 1: return c
return " "