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?
public class Solution {
public int singleNumber(int[] A) {
Arrays.sort(A);//can't access linear
//A.length must be odd
for(int i=0;i < A.length-1;i+=2){
if(A[i] != A[i+1])
return A[i];
}
//that's last element
return A[A.length-1];
}
}
Runtime:
436 ms
|
上述算法可能并非线性(有排序算法),但能通过验证???
不知,有无不排序找到single number的算法