[LeetCode] Single Number II

int singleNumber(int A[], int n)
{
    const int W = sizeof(int) * 8; // 整数字长
    int count[W]; // 每个位上1 出现的次数
    fill_n(&count[0], W, 0);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < W; j++)
        {
            count[j] += (A[i] >> j) & 1;
            count[j] %= 3;
        }
    }
    //展示数组中的结果
    for(int i = W-1; i >= 0; i--)
    {
        cout<<count[i];
    }
    cout<<endl;
    int result = 0;
    for (int i = 0; i < W; i++)
    {
        result += (count[i] << i);
    }
    return result;
}

你可能感兴趣的:([LeetCode] Single Number II)