136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

 

 

Bit Manipulation:

C++:

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4         int res = 0 ;
 5         for (int i : nums){
 6             res ^= i ;
 7         }
 8         return res ;
 9     }
10 };

 

 

java(1ms):

1 class Solution {
2     public int singleNumber(int[] nums) {
3         int res = 0 ;
4         for(int num : nums){
5             res ^= num ;
6         }
7         return res ;
8     }
9 }

 

posted @ 2016-10-27 23:35  __Meng  阅读(111)  评论(0编辑  收藏  举报