leetcode--Single Number II

来源:互联网 发布:见缝插针安卓源码 编辑:程序博客网 时间:2024/05/22 14:20

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;    }}

0 0