Lintcode 84. 落單的數 III
阿新 • • 發佈:2018-11-04
給出2*n + 2個的數字,除其中兩個數字之外其他每個數字均出現兩次,找到這兩個數字。
dalao解法:
public List<Integer> singleNumberIII(int[] A) { // write your code here //https://blog.csdn.net/wutingyehe/article/details/51085309?utm_source=copy if(null == A || A.length ==0) return null; List<Integer> list = new ArrayList<Integer>(); int x = 0; for(int i = 0; i < A.length; i++) { x ^= A[i]; } int k = 0; while((x % 2) == 0 && k < 32){ k++; x>>=1; } int result1 = 0, result2 = 0; for(int i = 0; i < A.length; i++) { if(((A[i]>>>k) & 1) == 0) { result1 ^= A[i]; }else { result2 ^= A[i]; } } list.add(result1); list.add(result2); return list; }
有點像分而治之