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?


自己的做法是先对数组进行一次排序,然后从前往后扫描统计个数。这样做虽然能AC,但是改变原有的数组,并不是好的方法

class Solution {
public:
    int singleNumber(int A[], int n) {
        if(n==0) exit(0);
        sort(A,A+n);
        int x = A[0];
        int times = 1;
        for(int i=1;i<n;++i){
            if(x==A[i]) {
                ++times;
                continue;
            }else{
                if(times == 1) break;
                x = A[i];
                times = 1;
            }
        }
        return x;
    }
};


以下是网上大家的方法。

1.用个数组记录各个位出现的次数

class Solution {
public:
    int singleNumber(int A[], int n) {
        if(!n) exit(0);
        int bits[32];
        memset(bits,0,sizeof(bits));
        int result=0;
        for(int i=0;i<n;++i){
            for(int j=0;j<sizeof(bits)/sizeof(int);++j){
                if((A[i]>>j)&1) ++bits[j];
            }
        }
        for(int j=0;j<sizeof(bits)/sizeof(int);++j){
            result |= ((bits[j]%3)<<j);
        }
        return result;
    }
};

2.思想:用三个掩码ones,twos,threes表示第i位出现了一次,两次,三次。但第i位出现3次时,把ones和twos重新置位。这里注意下ones,其做异或运算,当第i位出现1次时,第i位为1,两次时为0,三次时又为1。

class Solution {
public:
    int singleNumber(int A[], int n) {
         int ones = 0,twos = 0, threes = 0;
         for(int i=0;i<n;++i){
             twos |= ones&A[i];
             ones ^= A[i];
             threes =ones & twos;
             ones &= ~threes;
             twos &= ~threes;
         }
         return ones;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值