[LeetCode] NO. 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?
[题目解析] 如果对空间没有要求,可以使用map来解决。如果有要求的话,这里可以巧妙使用异或运算进行处理。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public int singleNumber(int[] nums) { 2 if(null != nums && nums.length > 0){ 3 int result = nums[0]; 4 for(int i = 1; i < nums.length; i++){ 5 result = result ^ nums[i]; 6 } 7 return result; 8 } 9 return -1; 10 }