剑指offer 35 第一个只出现一次的字符

来源:互联网 发布:汉诺塔递归算法python 编辑:程序博客网 时间:2024/06/11 08:28

题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置

思路

哈希,水题。

代码

# -*- coding:utf-8 -*-class Solution:    def FirstNotRepeatingChar(self, s):        # write code here        char_dict = {}        for c in s:            if c in char_dict.keys():                char_dict[c] += 1            else:                char_dict[c] = 1        for i, v in enumerate(s):            if char_dict[v] == 1:                return i        return -1
原创粉丝点击