LeetCode 137: Single Number II

12 篇文章 0 订阅

题目链接:

https://leetcode.com/problems/single-number-ii/description/

描述

Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

输入

输入一个整数数组,其中只有一个元素出现1次,其他元素均出现1次,找出这个仅出现过一次的元素。

输出

返回数组中仅出现一次的元素。

样例输入

[1,2,2,2,3,3,3]

样例输出

1

算法思想:

这道题特别巧妙,因为不能申请O(n)大小的内存,所以采用异或、与、非逻辑运算。当数第一次出现时,保存至ones中,twos为0,当数第二次出现时,清空ones,将数保留至twos中,当数第三次出现时,试图将数保留至ones,当时由于twos有值,故会清空ones中的值,twos也被清空。

转化成二进制其实好理解点,(参考他人解释:)
However, if you consider the problem in Boolean algebra form, everything becomes clear.

What we need to do is to store the number of ‘1’s of every bit. Since each of the 32 bits follow the same rules, we just need to consider 1 bit. We know a number appears 3 times at most, so we need 2 bits to store that. Now we have 4 state, 00, 01, 10 and 11, but we only need 3 of them.

In this solution, 00, 01 and 10 are chosen. Let ‘ones’ represents the first bit, ‘twos’ represents the second bit. Then we need to set rules for ‘ones’ and ‘twos’ so that they act as we hopes. The complete loop is 00->10->01->00(0->1->2->3/0).

For ‘ones’, we can get ‘ones = ones ^ A[i]; if (twos == 1) then ones = 0’, that can be tansformed to ‘ones = (ones ^ A[i]) & ~twos’.

Similarly, for ‘twos’, we can get ‘twos = twos ^ A[i]; if (ones* == 1) then twos = 0’ and ‘twos = (twos ^ A[i]) & ~ones’. Notice that ‘ones*’ is the value of ‘ones’ after calculation, that is why twos is
calculated later.

Here is another example. If a number appears 5 times at most, we can write a program using the same method. Now we need 3 bits and the loop is 000->100->010->110->001. The code looks like this:

int singleNumber(int A[], int n) {
    int na = 0, nb = 0, nc = 0;
    for(int i = 0; i < n; i++){
        nb = nb ^ (A[i] & na);
        na = (na ^ A[i]) & ~nc;
        nc = nc ^ (A[i] & ~na & ~nb);
    }
    return na & ~nb & ~nc;
}

Or even like this:

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

源代码

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int sz = nums.size(), ones = 0,twos = 0;                                                                                       
        for (int i = 0; i < sz; i++)
        {
            ones = (ones ^ nums[i]) & (~twos);
            twos = (twos ^ nums[i]) & (~ones);
        }
        return ones;
    }
};

算法复杂度:

由源代码可知,算法复杂度为O(n),空间复杂度为O(1)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值