用了一个字典
class Solution: def firstUniqChar(self, s: str) -> int: charIndexDict = {} for i in range(len(s)): if s[i] in charIndexDict: charIndexDict[s[i]] = -1 else: charIndexDict[s[i]] = i minIdx = float('inf') for char in charIndexDict: if charIndexDict[char] != -1: idx = charIndexDict[char] if idx < minIdx: minIdx = idx if minIdx == float('inf'): return -1 return minIdx