题目链接

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         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         int res = 0;
 6         for(int i = 0; i < n; i++)
 7             res = res^A[i];
 8         return res;
 9     }
10 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3422329.html

相关文章:

  • 2021-10-02
  • 2022-12-23
  • 2021-05-19
  • 2021-10-03
  • 2021-05-24
  • 2021-05-05
  • 2021-12-23
猜你喜欢
  • 2021-06-10
  • 2021-09-17
  • 2022-02-15
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
相关资源
相似解决方案