第一次遍历:
建立哈希表,以单个字符为Key,以字符出现次数为Value
第二次遍历:
搜索出现次数为1的字符,返回位置;若查完都无,则返回-1
# -*- coding:utf-8 -*- class Solution: def FirstNotRepeatingChar(self, s): res = {} for i in range(len(s)): if res.get(s[i]) == None: res[s[i]] = 1 else: res[s[i]] += 1 # print(res) for i in range(len(res)): if res[s[i]] == 1: # print(i) return i return -1