欢迎来到IT嘟嘟的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

第一个只出现一次的字符字符(python)

题目描述

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
 
# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        #字符为空则返回-1
        #通过字典存字符,如果唯一存在,则其恒为1
        len1= len(s)
        if len1<0:
            return -1
        dict = {}
        for j in s:
            dict[j] = 1 if j not in dict else dict[j] + 1
        for i in range(len1):
            if dict[s[i]] == 1:
                return i
        return -1

  

posted on 2019-03-26 11:07  IT嘟嘟  阅读(814)  评论(0编辑  收藏  举报

导航