您的位置:首页 > 编程语言

编程练习——第一个仅出现一次的字符

2017-08-03 21:36 295 查看


题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置

Python 1:

class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
l=len(s)
if l<1 or l>10000:
return -1
temp=""
for i in range(l):
if s[i] not in s[i+1:] and s[i] not in temp:
return i
temp+=s[i]
return -1

Python 2:用count()函数
import sys

while True:
try:
line=sys.stdin.next().strip()
for i in line:
if line.count(i)==1:
print i
break
except:
break
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 编程 字符串