[LeetCode] Number Complement 補數


 

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

 

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

 

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

 

這道題給了我們一個數,讓我們求補數。通過分析題目匯總的例子,我們知道需要做的就是每個位翻轉一下就行了,但是翻轉的起始位置上從最高位的1開始的,前面的0是不能被翻轉的,所以我們從高往低遍歷,如果遇到第一個1了后,我們的flag就賦值為true,然后就可以進行翻轉了,翻轉的方法就是對應位異或一個1即可,參見代碼如下:

 

解法一:

class Solution {
public:
    int findComplement(int num) {
        bool start = false;
        for (int i = 31; i >= 0; --i) {
            if (num & (1 << i)) start = true;
            if (start) num ^= (1 << i);
        }
        return num;
    }
};

 

由於位操作里面的取反符號~本身就可以翻轉位,但是如果直接對num取反的話就是每一位都翻轉了,而最高位1之前的0是不能翻轉的,所以我們只要用一個mask來標記最高位1前面的所有0的位置,然后對mask取反后,與上對num取反的結果即可,參見代碼如下:

 

解法二:

class Solution {
public:
    int findComplement(int num) {
        int mask = INT_MAX;
        while (mask & num) mask <<= 1;
        return ~mask & ~num;
    }
};

 

再來看一種迭代的寫法,一行搞定碉堡了,思路就是每次都右移一位,並根據最低位的值先進行翻轉,如果當前值小於等於1了,就不用再調用遞歸函數了,參見代碼如下:

 

解法三:

class Solution {
public:
    int findComplement(int num) {
        return (1 - num % 2) + 2 * (num <= 1 ? 0 : findComplement(num / 2));
    }
};

 

參考資料:

https://discuss.leetcode.com/topic/74627/3-line-c

https://discuss.leetcode.com/topic/74968/simple-java-one-line-solution

https://discuss.leetcode.com/topic/74642/java-1-line-bit-manipulation-solution

 

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


免責聲明!

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



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