解法1:建立哈希表,第一次扫描的时候,统计每个字符的出现次数。第二次扫描的时候,如果该字符出现次数为1,则返回这个字符的位置!
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if len(s) == 0:
return -1
hashmap = [0]*256
for i in range(len(s)):
hashmap[ord(s[i])] += 1
for i in range(len(s)):
if hashmap[ord(s[i])] == 1:
return i
return -1