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
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务