剑指Offer 50 第一个只出现一次的字符

第一个只出现一次的字符

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).

 1 # -*- coding:utf-8 -*-
 2 from collections import OrderedDict
 3 class Solution:
 4     def FirstNotRepeatingChar(self, s):
 5         n = len(s)
 6         if n == 0:
 7             return -1
 8         if n == 1:
 9             return -1
10         dic = OrderedDict()
11         for c in s:
12             if c in dic:
13                 dic[c] += 1
14             else:
15                 dic[c] = 1
16         for i in range(n):
17             if dic[s[i]] == 1:
18                 return i
19         return -1
20         # write code here
原文地址:https://www.cnblogs.com/asenyang/p/11022988.html