###### [leetcode] 231. Power of Two

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

Solution 1

Idea: divide n by 2, and check the residual.

class Solution {
public:
bool isPowerOfTwo(int n) {
while (n!=0 && n%2==0){
n = n/2;
}
return n==1;

}
};

Solution 2

Idea: 1 -- 1, 2 -- 10, 4 -- 100,... the highest bits are all 1. If minus 1, the left bit become 0 ,and others become 1s. Do bit logic and, should be 0;

class Solution {
public:
bool isPowerOfTwo(int n) {
return (n>0) && ((n&(n-1))==0);
}
};

#### leetcode 231: Power of Two

2015-07-07 05:01:24

#### LeetCode 231: Power of Two

2015-07-06 18:34:20

#### leetcode 231. Power of Two 判断是否为2的幂 Java

2016-04-23 16:38:25

#### leetcode 231 Power of Two C++

2016-05-23 17:10:54

#### 231. Power of Two [easy] (Python)

2016-05-05 14:47:26

#### Leetcode#231_power of two

2016-04-07 21:39:28

#### [LeetCode-231] Power of Two（判断一个数是不是2的若干次幂）

2015-09-14 19:59:45

#### 231. Power of Two（C++）

2017-05-10 21:01:45

#### leetcode:231. Power of Two

2017-08-01 00:55:54

#### Leetcode 231. Power of Two

2016-01-22 04:48:46

## 不良信息举报

[leetcode] 231. Power of Two