Given a non-empty 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?

Example 1:

Input: [2,2,1]
Output: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4

 
 
 
1 class Solution {
2 public:
3     int singleNumber(vector<int>& nums) {
4         int a = nums[0];
5         for(int i = 1; i < nums.size();i++)
6             a^=nums[i];
7         return a;
8     }
9 };

 

相关文章:

  • 2021-11-04
  • 2021-07-25
  • 2021-06-10
  • 2021-06-11
猜你喜欢
  • 2022-12-23
  • 2022-02-04
  • 2021-10-25
  • 2021-04-30
相关资源
相似解决方案