什么是找到其中仅出现一次在一个字符串的第一个字符的最快方法?
What is the quickest way to find the first character which only appears once in a string?
您可以不知道该字符是未重复,直到你处理整个字符串,所以我的建议是这样的:
You can't know that the character is un-repeated until you've processed the whole string, so my suggestion would be this:
def first_non_repeated_character(string):
chars = []
repeated = []
for character in string:
if character in chars:
chars.remove(character)
repeated.append(character)
else:
if not character in repeated:
chars.append(character)
if len(chars):
return chars[0]
else:
return False
编辑:最初发布code很糟糕,但这个最新的片断认证工作在瑞安的电脑™
originally posted code was bad, but this latest snippet is Certified To Work On Ryan's Computer™.