[LeetCode] 137. Single Number II 單獨的數字之二


 

Given a non-empty 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?

Example 1:

Input: [2,2,3,2]
Output: 3

Example 2:

Input: [0,1,0,1,0,1,99]
Output: 99

 

這道題是之前那道 Single Number 的延伸,那道題的解法就比較獨特,是利用計算機按位儲存數字的特性來做的,這道題就是除了一個單獨的數字之外,數組中其他的數字都出現了三次,還是要利用位操作 Bit Manipulation 來解。可以建立一個 32 位的數字,來統計每一位上1出現的個數,如果某一位上為1的話,那么如果該整數出現了三次,對3取余為0,這樣把每個數的對應位都加起來對3取余,最終剩下來的那個數就是單獨的數字。代碼如下:

 

解法一:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for (int i = 0; i < 32; ++i) {
            int sum = 0;
            for (int j = 0; j < nums.size(); ++j) {
                sum += (nums[j] >> i) & 1;
            }
            res |= (sum % 3) << i;
        }
        return res;
    }
};

 

還有一種解法,思路很相似,用3個整數來表示 INT 的各位的出現次數情況,one 表示出現了1次,two 表示出現了2次。當出現3次的時候該位清零。最后答案就是one的值。

  1. ones   代表第 ith 位只出現一次的掩碼變量
  2. twos  代表第 ith 位只出現兩次次的掩碼變量
  3. threes  代表第 ith 位只出現三次的掩碼變量

假設現在有一個數字1,更新 one 的方法就是 ‘亦或’ 這個1,則 one 就變成了1,而 two 的更新方法是用上一個狀態下的 one 去 ‘與’ 上數字1,然后 ‘或’ 上這個結果,這樣假如之前 one 是1,那么此時 two 也會變成1,這 make sense,因為說明是當前位遇到兩個1了;反之如果之前 one 是0,那么現在 two 也就是0。注意更新的順序是先更新 two,再更新 one,不理解的話只要帶個只有一個數字1的輸入數組看一下就不難理解了。然后更新 three,如果此時 one 和 two 都是1了,由於先更新的 two,再更新的 one,two 為1,說明此時至少有兩個數字1了,而此時 one 為1,說明了此時已經有了三個數字1,這塊要仔細想清楚,因為 one 是要 ‘亦或’ 一個1的,值能為1,說明之前 one 為0,實際情況是,當第二個1來的時候,two 先更新為1,此時 one 再更新為0,下面 three 就是0了,那么 ‘與’ 上t hree 的相反數1不會改變 one 和 two 的值;那么當第三個1來的時候,two 還是1,此時 one 就更新為1了,那么 three 就更新為1了,此時就要清空 one 和 two 了,讓它們 ‘與’ 上 three 的相反數0即可,最終結果將會保存在 one 中,參見代碼如下:

 

解法二:

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

 

下面這種解法思路也十分巧妙,根據上面解法的思路,我們把數組中數字的每一位累加起來對3取余,剩下的結果就是那個單獨數組該位上的數字,由於累加的過程都要對3取余,那么每一位上累加的過程就是 0->1->2->0,換成二進制的表示為 00->01->10->00,可以寫出對應關系:

00 (+) 1 = 01

01 (+) 1 = 10

10 (+) 1 = 00 ( mod 3)

用 ab 來表示開始的狀態,對於加1操作后,得到的新狀態的 ab 的算法如下:

b = b xor r & ~a;

a = a xor r & ~b;

這里的 ab 就是上面的三種狀態 00,01,10 的十位和各位,剛開始的時候,a和b都是0,當此時遇到數字1的時候,b更新為1,a更新為0,就是 01 的狀態;再次遇到1的時候,b更新為0,a更新為1,就是 10 的狀態;再次遇到1的時候,b更新為0,a更新為0,就是 00 的狀態,相當於重置了;最后的結果保存在b中,明白了上面的分析過程,就能寫出代碼如下;

 

解法三:

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

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/137

 

類似題目:

Single Number

Single Number III

 

參考資料:

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

https://leetcode.com/problems/single-number-ii/discuss/43294/Challenge-me-thx

https://leetcode.com/problems/single-number-ii/discuss/43296/An-General-Way-to-Handle-All-this-sort-of-questions.

https://leetcode.com/problems/single-number-ii/discuss/43297/Java-O(n)-easy-to-understand-solution-easily-extended-to-any-times-of-occurance

https://leetcode.com/problems/single-number-ii/discuss/43295/Detailed-explanation-and-generalization-of-the-bitwise-operation-method-for-single-numbers

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM