Skip to content

Commit 2462743

Browse files
committed
🚀 05-Aug-2020
1 parent 59e7261 commit 2462743

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
2+
3+
Example 1:
4+
5+
Input: 16
6+
Output: true
7+
Example 2:
8+
9+
Input: 5
10+
Output: false
11+
Follow up: Could you solve it without loops/recursion?
12+
13+
14+
15+
16+
17+
18+
19+
20+
class Solution {
21+
public:
22+
bool isPowerOfFour(int num) {
23+
if(num<=0) return false;
24+
if(num==1) return true; // 4^0=1
25+
while(num>1){
26+
if(num%4!=0) return false;
27+
num/=4;
28+
}
29+
return true;
30+
}
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
2+
3+
Example 1:
4+
5+
Input: 16
6+
Output: true
7+
Example 2:
8+
9+
Input: 5
10+
Output: false
11+
Follow up: Could you solve it without loops/recursion?
12+
13+
14+
15+
16+
17+
18+
19+
20+
class Solution {
21+
public:
22+
bool isPowerOfFour(int num) {
23+
if(num<=0) return false;
24+
if(num==1) return true; // 4^0=1
25+
while(num>1){
26+
if(num%4!=0) return false;
27+
num/=4;
28+
}
29+
return true;
30+
}
31+
};

0 commit comments

Comments
 (0)