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

来源:互联网 发布:阿玛尼没有入住淘宝吗 编辑:程序博客网 时间:2024/06/11 16:23

题目描述

在一个字符串(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


原创粉丝点击