第一个只出现一次的字符字符(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

  

原文地址:https://www.cnblogs.com/277223178dudu/p/10598871.html