1. 程式人生 > >231. Power of Two

231. Power of Two

sym eth 常數 determine where 一行 htm boolean 分析

Given an integer, write a function to determine if it is a power of two.

這道題讓我們判斷一個數是否為2的次方數,而且要求時間和空間復雜度都為常數,那麽對於這種玩數字的題,我們應該首先考慮位操作 Bit Operation。在LeetCode中,位操作的題有很多,比如比如Repeated DNA Sequences 求重復的DNA序列, Single Number 單獨的數字, Single Number II 單獨的數字之二 , Grey Code 格雷碼, Reverse Bits 翻轉位,Bitwise AND of Numbers Range 數字範圍位相與,Number of 1 Bits 位1的個數和 Divide Two Integers 兩數相除等等。那麽我們來觀察下2的次方數的二進制寫法的特點:

1 2 4 8 16   ....

1 10 100 1000 10000 ....

那麽我們很容易看出來2的次方數都只有一個1,剩下的都是0,所以我們的解題思路就有了,我們只要每次判斷最低位是否為1,然後向右移位,最後統計1的個數即可判斷是否是2的次方數,代碼如下:

解法一:

public class Solution {
    public boolean isPowerOfTwo(int n) {
        return n>0 && Integer.bitCount(n) == 1;
    }
}

這道題還有一個技巧,如果一個數是2的次方數的話,根據上面分析,那麽它的二進數必然是最高位為1,其它都為0,那麽如果此時我們減1的話,則最高位會降一位,其余為0的位現在都為變為1,那麽我們把兩數相與,就會得到0,用這個性質也能來解題,而且只需一行代碼就可以搞定,如下所示:

This question is not an difficult one, and there are many ways to solve it.

Method 1: Iterative

check if n can be divided by 2. If yes, divide n by 2 and check it repeatedly.

if(n==0) return false;
while(n%2==0) n/=2;
return (n==1);

Time complexity = O(log n)

Method 2: Recursive

return n>0 && (n==1 || (n%2==0 && isPowerOfTwo(n/2)));

Time complexity = O(log n)

Method 3: Bit operation

If n is the power of two:

  • n = 2 ^ 0 = 1 = 0b0000...00000001, and (n - 1) = 0 = 0b0000...0000.
  • n = 2 ^ 1 = 2 = 0b0000...00000010, and (n - 1) = 1 = 0b0000...0001.
  • n = 2 ^ 2 = 4 = 0b0000...00000100, and (n - 1) = 3 = 0b0000...0011.
  • n = 2 ^ 3 = 8 = 0b0000...00001000, and (n - 1) = 7 = 0b0000...0111.

we have n & (n-1) == 0b0000...0000 == 0

Otherwise, n & (n-1) != 0.

For example, n =14 = 0b0000...1110, and (n - 1) = 13 = 0b0000...1101.

return n>0 && ((n & (n-1)) == 0);

Time complexity = O(1)

Method 4: Math derivation

Because the range of an integer = -2147483648 (-2^31) ~ 2147483647 (2^31-1), the max possible power of two = 2^30 = 1073741824.

(1) If n is the power of two, let n = 2^k, where k is an integer.

We have 2^30 = (2^k) * 2^(30-k), which means (2^30 % 2^k) == 0.

(2) If n is not the power of two, let n = j*(2^k), where k is an integer and j is an odd number.

We have (2^30 % j*(2^k)) == (2^(30-k) % j) != 0.

return n>0 && (1073741824 % n == 0);

Time complexity = O(1)

231. Power of Two