查找数组中的特殊数字
数组中有很多数字,除了一个特殊数字出现一次外,每个数字都出现3次。问题是:如何找到数组中的特殊数字?
现在我只能提出一些基数排序和快速排序的方法,无法利用问题的性质。所以我需要一些其他的算法。
感谢您的帮助。
There are many numbers in an array and each number appears three times excepting for one special number appearing once. Here is the question: how can I find the special number in the array?
Now I can only put forward some methods with radix sorting and rapid sorting which cannot takes advantage the property of the question. So I need some other algorithms.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将数字按位 mod 3 添加,例如
Add the numbers bitwise mod 3, e.g.
既然没人说,我就说:哈希表。
您可以使用简单的哈希表(或哈希图)计算每个元素在数组中出现的次数,时间复杂度为 O(n)。
Since nobody's saying it, I will: hashtable.
You can calculate how many times each element occurs in the array in
O(n)
with simple hashtable (or hashmap).如果数组已排序,则问题很简单,您只需循环遍历列表,一次三个项目,并检查第三个项目是否与当前项目相同。
如果数组没有排序,可以使用哈希表来统计每个数字出现的次数。
If the array is sorted, the problem is trivial, you just loop through the list, three items at a time, and check if the third item is the same as the current.
If the array is not sorted, you can use a Hash Table to count the number of occurences of each numbers.
一种可能的算法(非常通用,未经测试):
A possible algorithm (very generic, not tested) :
另一种时间复杂度为O(n)、额外空间为O(1)的方法
以下是aj建议的 。我们可以将所有数字相同位置的位相加,并与 3 取模。
总和不是 3 的倍数的位是该数字中出现一次的位。
让我们考虑
示例数组 {5, 5, 5, 8}。
101, 101, 101, 1000
第一位的和%3 = (1 + 1 + 1 + 0)%3 = 0;
第二位之和%3 = (0 + 0 + 0 + 0)%0 = 0;
第三位之和%3 = (1 + 1 + 1 + 0)%3 = 0;
第四位之和%3 = (1)%3 = 1;
因此出现一次的数字是1000
Following is another O(n) time complexity and O(1) extra space method
suggested by aj. We can sum the bits in same positions for all the numbers and take modulo with 3.
The bits for which sum is not multiple of 3, are the bits of number with single occurrence.
Let us consider
the example array {5, 5, 5, 8}.
The 101, 101, 101, 1000
Sum of first bits%3 = (1 + 1 + 1 + 0)%3 = 0;
Sum of second bits%3 = (0 + 0 + 0 + 0)%0 = 0;
Sum of third bits%3 = (1 + 1 + 1 + 0)%3 = 0;
Sum of fourth bits%3 = (1)%3 = 1;
Hence number which appears once is 1000
下面的怎么样?
如果我们假设您知道数组中所有数字的最大值和最小值(或者至少可以将它们限制在某个最大范围内,例如 max - min + 1,然后创建一个该大小的辅助数组,并初始化为全零, 现在扫描您的原始数组,例如 MyArray [
],并为每个元素 MyArray[i] 递增
AuxArray[MyArray[i]] 加一。扫描完成后,将只有一个元素
AuxArray[] 中的值等于 1,并且 AuxArray[] 中该元素的索引将是该特殊数字的值。
这里没有复杂的搜索。只是复杂性的线性顺序。
希望我说得有道理。
约翰·多纳
How about the following?
If we assume that you know the maximum and minimum values of all numbers in the array (or can at least limit them to some maximum range, say max - min + 1, then create an auxiliary array of that size, initialized to all zeros, say AuxArray[].
Now scan your original array, say MyArray[], and for each element MyArray[i], increment
AuxArray[MyArray[i]] by one. After your scan is complete, there will be exactly one element
in AuxArray[] that equals one, and the index of that element in AuxArray[] will be the value of the special number.
No complicated search here. Just a linear order of complexity.
Hope I've made sense.
John Doner
我没有发现按位 mod 3 的实现非常直观,因此我编写了一个更直观的代码版本,并使用各种示例对其进行了测试,并且它有效。
这是循环内的代码
希望你们很容易理解这个版本的代码。
I didnt find the implementation of bitwise mod 3 very intuitive so I wrote a more intiuitive version of the code and tested it with various examples and it worked.
Here is the code inside the loop
Hope you guys find it easy to understand this version of code.
我得到了一个解决方案。时间为 O(n),空间为 O(1)。
I got a solution. It's O (n) time and O (1) space.