50. 第一个只出现一次的字符

第一个只出现一次的字符

http://www.nowcoder.com/questionTerminal/1c82e8cf713b4bbeb2a5b31cf5b0417c

注意python3直接字典遍历是有序的,而python2需要用有序字典

from collections import OrderedDict
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        hashtable = OrderedDict()
        for i in s:
            if i in hashtable:
                hashtable[i] += 1
            else:
                hashtable[i] = 1
        for j in hashtable:
            if hashtable[j] == 1:
                return s.index(j)
        return -1
全部评论

相关推荐

点赞 评论 收藏
转发
4 2 评论
分享
牛客网
牛客企业服务