当前位置:网站首页>[daily training -- Tencent select 50] 231 Power of 2

[daily training -- Tencent select 50] 231 Power of 2

2022-07-07 13:38:00 Puppet__

subject

Give you an integer n, Please judge whether the integer is 2 Power square . If it is , return true ; otherwise , return false .

If there is an integer x bring n == 2x , Think n yes 2 Power square .

Example 1:
Input :n = 1
Output :true
explain :20 = 1

Example 2:
Input :n = 16
Output :true
explain :24 = 16

Example 3:
Input :n = 3
Output :false

Example 4:
Input :n = 4
Output :true

Example 5:
Input :n = 5
Output :false

Tips :
-231 <= n <= 231 - 1

Code

package tencent50;

public class leetcode231 {
    

    //  In conformity with the regulations n The maximum value is 2^30, So we just need to judge the current n Is it right? 2^30 It's a divisor of 
    int bigNum = 1 << 30;
    public boolean isPowerOfTwo(int n) {
    
        return n > 0 && bigNum % n == 0;
    }
    //  Or is it 2 If it's a power of , Then it has only one binary digit 1
    public boolean isPowerOfTwo1(int n) {
    
        return n > 0 && (n & (n - 1)) == 0;
    }

    public static void main(String[] args) {
    
        leetcode231 obj = new leetcode231();
        System.out.println(obj.isPowerOfTwo(3));
    }
}

原网站

版权声明
本文为[Puppet__]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071137347728.html