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 }
原文地址:https://www.cnblogs.com/mengchunchen/p/6006131.html