Single Number II

来源:互联网 发布:教师网络研修计划 编辑:程序博客网 时间:2024/06/06 17:31

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?'

思路:统计32个bit的出现次数,最后%3,就是单数个数的bit;

public class Solution {    public int singleNumber(int[] nums) {        int[] bitcount = new int[32];        int res = 0;        for(int i=0; i<32; i++){            for(int j = 0; j < nums.length; j++){                bitcount[i] += (nums[j]>>i & 1);            }             res |= (bitcount[i]%3)<<i;        }        return res;    }}


0 0
原创粉丝点击