136. Single Number(位运算)



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 };

 

posted @ 2019-01-16 15:26  乐乐章  阅读(130)  评论(0编辑  收藏  举报