-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path231_power_of_two.cpp
More file actions
65 lines (50 loc) · 1.08 KB
/
Copy path231_power_of_two.cpp
File metadata and controls
65 lines (50 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n = 1;
Solution solve;
bool result = solve.isPowerOfTwo(n);
if (result) {
cout << "true\n";
} else {
cout << "false";
}
return 0;
}
/*
------------------
Problem Statement:
------------------
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2^x.
----------
Example 1:
----------
Input: n = 1
Output: true
Explanation: 2^0 = 1
----------
Example 2:
----------
Input: n = 16
Output: true
Explanation: 2^4 = 16
----------
Example 3:
----------
Input: n = 3
Output: false
------------
Constraints:
------------
-2^31 <= n <= 2^31 - 1
Follow up: Could you solve it without loops/recursion?
*/