每天一刷,强迫学习
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?
从提示来看,1、时间线性增加;2、不使用额外空间。可见常规的手段如额外开辟数组之类的方法无法实现。想的焦头烂额,实际上一点即透。实现方法就是异或。
public class Solution {
public int singleNumber(int[] A) {
int temp = 0;
for(int i = 0; i < A.length; i++){
temp = temp^A[i];
}
return temp;
}
}