在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
- 代码一
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if not s:
return -1
tmpList = []
for i in s:
tmpList.append(s.count(i))
return tmpList.index(1)
- 代码二
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if s == '':
return -1
ret = []
for i in range(len(s)):
count = 0
for j in range(len(s)):
if s[i] == s[j]:
count += 1
ret.append(count)
for i in range(len(ret)):
if ret[i] == 1:
return i
return -1