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?
- public class Solution {
- public int singleNumber(int[] nums) {
- int len = Integer.SIZE;
- int res = 0;
- int[] arr = new int[len];
- for(int i=0;i<nums.length;i++){
- for(int j=0;j<len;j++){
- arr[j] += (nums[i]>>j)&1;
- arr[j] %= 3;
- }
- }
- for(int i=0;i<len;i++){
- res += (arr[i]<<i);
- }
- return res;
- }
- }
原文链接http://blog.youkuaiyun.com/crazy__chen/article/details/46563271