原题:
Given an array of integers, every element appears twice except for one. Find that single one.
=>找出数组中只出现一次的元素。注:其它元素都出现过两次。
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
注意:
=>你的算法需要有一个线性的复杂度。即o(n)。最好不要使用额外的memory来实现。
class Solution { public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each } };
晓东分析:
这个题目的关键在于线性的复杂度,这不由得让我们去想别的都是出现两次这样的条件。我们开始想,把这些一样都减掉不就只剩那个出现过一次的元素了?可惜我们无法实现那个把他们都减掉的算法,没关系,我们突然想到了一个更好的方法,就是相同的数除了相减还有一种方法结果为0,那就是异或。这样的话,因为相同的元素都被异或为0了,而任何数和0异或结果还是它本身,所以我们把所有的元素都一起异或,不就剩了那个单独的元素了么?
代码实现:
class Solution { public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int result = A[0]; if(n == 1) return result; for(int i = 1; i < n; i++) result ^= A[i]; return result; } };
这一段代码的执行,在测试库中的结果:
14 / 14test cases passed.
|
Status: Accepted |
Runtime:
48 ms
|
过了全部的14个case,执行的时间是48ms,算过了。
希望大家有更好的算法能够提出来,不甚感谢。
若您觉得该文章对您有帮助,请在下面用鼠标轻轻按一下“顶”,哈哈~~·