Problem
Given an integer, write a function to determine if it is a power of two.
Example
Input: 1
Output: true
Explanation: 20 = 1
Input: 16
Output: true
Explanation: 24 = 16
Input: 218
Output: false
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n==1)
return true;
long i=2;
while(i<n){
i=i*2;
}
if(i==n)
return true;
return false;
}
};
Result
![](https://img.haomeiwen.com/i140476/756db9f053861ca6.png)
网友评论