题目:
在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
思路:
建立一个哈希表即可,python用字典代替,但是是无序的,所以再遍历一次字符串
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
if len(s) <= 0:
return -1
hash_dict = {}
for i in s:
if i in hash_dict:
hash_dict[i] += 1
else:
hash_dict[i] = 1
for j in s:
if hash_dict[j] == 1:
return s.index(j)
# write code here