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

来源:互联网 发布:js input name 数组 编辑:程序博客网 时间:2024/05/22 13:35

题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置。
def firstnotrepeat(str):    hashdict = {}    for index,item in enumerate(str):        if item in hashdict:            hashdict[item] += 1        else:            hashdict[item] = 1    for index ,item in enumerate(str):        if (hashdict[item]==1):            return index    return -1if __name__ == '__main__':    a = firstnotrepeat('google')    print(a)


原创粉丝点击