思路:查表法,首先建立字典,遍历字符串判断字典中的值是否为1,若为1,则返回。
Python:
class Solution: def firstUniqChar(self, s: str) -> str: if not s: return ' ' from collections import Counter tmp_s=Counter(s) for i in range(len(s)): if tmp_s[s[i]]==1: return s[i] return ' '
class Solution: def firstUniqChar(self, s: str) -> str: if not s: return ' ' from collections import Counter tmp_s=Counter(s) for i in range(len(s)): if tmp_s[s[i]]==1: return s[i] return ' '