[easy]Power of Two

难度: 简单 标题:2的冥

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

判断一个是否是2的幂。

思路

一个数转换成二进制后,假如此数为2的冥,则它的形式一定是100…000。

所以只要不断右移,计算1出现的次数。n & 1 ,假如 n 末尾为1,则结果为1.末尾为 0,结果为 0;

代码

1
2
3
4
5
6
7
8
9
10
public class Solution {
public boolean isPowerOfTwo(int n) {
int ctz = 0;
while(n > 0){
ctz += (n & 1);
n = n >> 1;
}
return ctz == 1;
}
}

链接

https://leetcode.com/problems/power-of-two/

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×