查找字符串的第一个非重复的字符 [英] Find the first un-repeated character in a string
本文介绍了查找字符串的第一个非重复的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是找到其中仅出现一次在一个字符串的第一个字符的最快方法?
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™.
这篇关于查找字符串的第一个非重复的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文