File tree 2 files changed +62
-0
lines changed
competitive programming/leetcode
2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments