136. Single Number

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?

思路:
 
使用异或逻辑操作符, 即: a ^ a= 0 , a ^ 0 =a , (a ^ b) ^ c = a ^ (b ^ c) ;
 
1  int singleNumber(vector<int>& nums) {
2         int val=0;
3          for(int num:nums)
4             val=val^num;
5          return val;
6     }
posted @ 2018-03-05 10:45  SuperStar~  阅读(80)  评论(0编辑  收藏  举报