标签:
Given 3*n + 1
numbers, every numbers occurs triple times except one, find it.
Given [1,1,2,3,3,3,2,2,4,1]
return 4
One-pass, constant extra space.
public class Solution { /** * @param A : An integer array * @return : An integer */ public int singleNumberII(int[] A) { // write your code here if(A == null || A.length == 0) return 0; int[] count = new int[32]; for(int i = 0; i < 32; i++){ for(int j = 0; j < A.length; j++){ count[i] += (A[j] >> i) & 1; count[i] %= 3; } } int res = 0; for(int i = 0; i < 32; i++) res = res | (count[i] << i); return res; } }
lintcode-medium-Single Number II
标签:
原文地址:http://www.cnblogs.com/goblinengineer/p/5357668.html