你怎么能有效地从一个字符串,删除重复的字符? [英] How can you efficiently remove duplicate characters from a string?
本文介绍了你怎么能有效地从一个字符串,删除重复的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能去除从字符串复制的字符不保存你见过一个数组中的每个字符和检查,看看是否有新的角色已在该阵列?这似乎是非常低效的。当然,必须有一个更快的方法?
Is it possible to remove duplicate characters from a string without saving each character you've seen in an array and checking to see if new characters are already in that array? That seems highly inefficient. Surely there must be a quicker method?
推荐答案
您可以使用布尔数组的按字符索引的:
You can use a boolean array indexed by character:
bool seen[256];
有关字节大小的ASCII般的人物,上面是适当的。对于16位统一code:
For byte-sized ASCII-like characters, the above would be appropriate. For 16-bit Unicode:
bool seen[65536];
等。然后,对字符串中的每个字符它是一个简单的查找,以查看是否该布尔已设定
and so on. Then, for each character in the string it's a simple lookup to see whether that boolean has already been set.
这篇关于你怎么能有效地从一个字符串,删除重复的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文