标签:
Single Number II
问题:
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路:
hashtable 容易想到
bit vector 新思路
他人代码:
public class Solution { public int singleNumber(int[] A) { /* element in A is 32bit, sum corresponding bits from all elements and mod each by 3 then should left the single number */ int[] sum=new int[32]; int res=0; for(int i=0;i<32;i++) { for(int j=0;j<A.length;j++) { sum[i]+=((A[j]>>i)&1);//sum every bit of all numbers } sum[i]%=3; res+=((sum[i]&1)<<i);// recover the single number } return res; } }
学习之处:
标签:
原文地址:http://www.cnblogs.com/sunshisonghit/p/4316222.html