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?

异或即可处理,而且,不仅能处理两次的情况,只要出现偶数次,都可以清零。

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         
 5         int res = 0;
 6         
 7         for(int i = 0; i<n; i++)
 8         {
 9             res = res ^ A[i];
10         }
11         
12         return res;
13     }
14 };

 

相关文章:

  • 2022-12-23
  • 2021-06-23
  • 2021-08-04
  • 2021-06-12
  • 2021-09-26
  • 2022-01-01
猜你喜欢
  • 2022-12-23
  • 2021-09-21
  • 2021-05-18
  • 2021-09-26
  • 2021-11-03
相关资源
相似解决方案