您的位置:首页 > 其它

leetcode--Single Number II

2017-08-08 12:43 337 查看
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?

[java] view
plain copy

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.csdn.net/crazy__chen/article/details/46563271
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: