342.Power of Four(Easy)

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
給定一個(gè)32位的整數(shù),判斷是否為4次冪

  一開始看到這題我就想到打表,但是,果然還是位運(yùn)算最厲害

Example:

Given num = 16, return true. Given num = 5, return false.

Follow up

Could you solve it without loops/recursion?

My Solution

(Java) Version 1 Time: 2ms:

  沒有什么好說的,在32位數(shù)的世界里面,4次冪并沒有幾個(gè),只有15個(gè)數(shù),直接存數(shù)組里面對(duì)比就是了,只是沒有想到還是慢了

public class Solution {
    public boolean isPowerOfFour(int num) {
        int[] integer = new int[]{1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,16777216,67108864,268435456,1073741824};
        for(int i=0;i<16;i++){
            if(num==integer[i])return true;
        }
        return false;
    }
}

(Java) Version 2 Time: 3ms (By k'):

  不僅簡(jiǎn)潔還快,果然所有和2次冪扯上關(guān)系的,都可以讓位運(yùn)算來一腳

  老外的解釋:
  The mask is 01010101010101010101010101010101 (0x55555555), reason for that is any number that is power of 4 happens bit and other bit.

  For example for 8 bits:
  4 ^ 0 = 00000001 (first bit is set)
  4 ^ 1 = 00000100 ( third bit is set)
  4 ^ 2 = 00010000 (5th bit is set)
  and so on

  So the mask would be 0x55555555 (this will take care of the sign bit too)
  The other part is that you want to make sure that only one bit is set in the number (similar to numbers of power 2), this is done by num == (num & -num)
  num == (num & -num) will return true only if there was one bit set, the reason has to do about 2's complement representation.

  For example:
  4 = 00000100
  -4 = 11111011 + 1 = 11111100
  so 00000100 & 11111100 = 00000100

  The only exception for this rule is if num is 0 which explains why we check num != 0
  This solution would work for any number that is power of 2 (4, 8, 16, ..), you just need to adjust the mask

public class Solution {
    public boolean isPowerOfFour(int num) {
        return num != 0 && (num & 0x55555555) == num && num == (num & -num);
    }
}

(Java) Version 3 Time: 21ms (By yfcheng):

  有意思的從字符串出發(fā)的思路,雖然慢就慢了點(diǎn)
  老外的解釋:

The idea is that numbers in quaternary system that is power of 4 will be like 10, 100, 1000 and such. Similar to binary case. And this can be extended to any radix.

public class Solution {
    public boolean isPowerOfFour(int num) {
        return Integer.toString(num, 4).matches("10*");
    }
}

(Java) Version 4 Time: ms (By LeeLom):

  似乎是一個(gè)通用的解法?

public class Solution { 
    public boolean isPowerOfFour(int num) { 
        int x = (int)Math.sqrt(num); 
        //1073741824 is 4^15, 4^16 is bigger than int  
        return(num > 0 && 1073741824 % num == 0 && x*x == num); 
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,063評(píng)論 0 23
  • 新的學(xué)期開始了,星皓是三年級(jí)的學(xué)生了,從以前的兒童時(shí)期過渡到少年時(shí)期了。今年又增加了幾門新課,有英語、葫蘆絲...
    星皓媽媽閱讀 479評(píng)論 0 1
  • Spring Boot 是使用了“習(xí)慣優(yōu)于配置”(Spring Boot 會(huì)默認(rèn)給我們做一些全局配置)的理念,從而...
    司鑫閱讀 448評(píng)論 0 2
  • 我的家鄉(xiāng)產(chǎn)蓮子,叫宣蓮,是中國(guó)三大名蓮之一。新摘下來的蓮子一般是長(zhǎng)這個(gè)樣子的, 而最終能夠出售的是這樣的, 蓮子的...
    豪哥的世界閱讀 1,930評(píng)論 0 3
  • 早上給張小諾喂奶,可能是因?yàn)槟讨屑愉\粉的原因,口味太甜,小諾不喜歡,好說歹說人家就是不喝,奶奶喂不行,爸爸喂不行,...
    三千微閱讀 337評(píng)論 0 0

友情鏈接更多精彩內(nèi)容