【Leetcode】137. 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?

题解:

The code seems tricky and hard to understand at first glance.
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;
}

I hope all these above can help you have a better understand of this problem. (from here,and author is woshidaishu).

Solution 1 ()

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int ones = 0, twos = 0;
        for(auto n : nums){
            ones = (ones ^ n) & ~twos;
            twos = (twos ^ n) & ~ones;
        }
        return ones;
    }
};
时间: 2024-10-11 16:09:26

【Leetcode】137. Single Number II的相关文章

【一天一道LeetCode】#137. Single Number II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 complex

【LeetCode】136. Single Number 解题小结

题目: Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 这题目的要求不仅是要求是线性时间,希望也不会使用额外的内存,那么也就是你无法运用

【LeetCode】-- 260. Single Number III

问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, b). 要求常量空间,线性时间. 问题解决: 这题用到“神奇的位运算”. 1.因为除了特殊的两个元素,其余两两出现,那么就想到了XOR(异或). 2.从头到尾XOR之后,会得到a xor b 的结果.接下来我们试着把这两个元素分离开来. 3.我们在a xor b 中找到任意一位XOR[diff_po

【leetcode】260. Single Number III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The order

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find >the two elements that a

LeetCodeOJ 137. Single Number II 一题三解

原题链接:https://leetcode.com/problems/single-number-ii/ 题目如下: 137. Single Number II QuestionEditorial Solution My Submissions Total Accepted: 91049 Total Submissions: 235598 Difficulty: Medium Given an array of integers, every element appears three time

【Leetcode】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

【LeetCode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【Leetcode】Pascal&#39;s Triangle II

题目链接:https://leetcode.com/problems/pascals-triangle-ii/ 题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路: 要求空间复杂度为O