题目描述
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
解题思路:创建一个count计数器列表,将出现过的字符按照顺序添加至temp列表,然后统计每个字符出现的次数,返回第一个出现次数为1的字符val值,获取s中第一次出现val值的位置,并返回该结果res
-- coding:utf-8 --
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if s == "":
return -1
temp = list()
for string in s[:]:
if string not in temp:
temp.append(string)
count = [0]* len(temp)
for string in s[:]:
for t in temp:
if t == string:
count[temp.index(t)] = count[temp.index(t)] + 1
val = temp[count.index(1)]
res = list(s).index(val)
return res
京公网安备 11010502036488号