File tree 4 files changed +140
-0
lines changed
4 files changed +140
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ -* 342. Power of Four *-
4
+
5
+ Given an integer n, return true if it is a power of four. Otherwise, return false.
6
+
7
+ An integer n is a power of four, if there exists an integer x such that n == 4x.
8
+
9
+
10
+
11
+ Example 1:
12
+
13
+ Input: n = 16
14
+ Output: true
15
+ Example 2:
16
+
17
+ Input: n = 5
18
+ Output: false
19
+ Example 3:
20
+
21
+ Input: n = 1
22
+ Output: true
23
+
24
+
25
+ Constraints:
26
+
27
+ -231 <= n <= 231 - 1
28
+
29
+
30
+ Follow up: Could you solve it without loops/recursion?
31
+
32
+
33
+
34
+ */
35
+
36
+ import 'dart:math' ;
37
+
38
+ class A {
39
+ bool isPowerOfFour (int n) {
40
+ if (n <= 0 ) return false ;
41
+ while (n % 3 == 0 ) {
42
+ n ~ /= 3 ;
43
+ }
44
+ return n == 1 ;
45
+ }
46
+ }
47
+
48
+ class B {
49
+ bool isPowerOfFour (int n) {
50
+ return (log (n) / log (4 )) % 1 == 0 ;
51
+ }
52
+ }
53
+
54
+ class C {
55
+ bool isPowerOfFour (int n) {
56
+ if (n <= 1 ) return n == 1 ;
57
+ return n % 4 == 0 && isPowerOfFour (n ~ / 4 );
58
+ }
59
+ }
60
+
61
+ class D {
62
+ bool isPowerOfThree (int n) {
63
+ if (n < 1 ) return false ;
64
+ String nBase4 = "" ;
65
+ while (n != 0 ) nBase4 += (n % 4 ).toString ();
66
+ n ~ /= 3 ; // conversion to base 3
67
+ int i = 0 ;
68
+ while (i < nBase4.length - 1 )
69
+ if (nBase4[i++ ] != '0' )
70
+ return false ; // checking if all digits in base 3 converted number except first one are 0
71
+ return nBase4[i] == '1' ; // check if starting digit is 1
72
+ }
73
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ func isPowerOfFour (n int ) bool {
4
+ if n <= 0 {
5
+ return false
6
+ }
7
+
8
+ for n % 4 == 0 {
9
+ n /= 4
10
+ }
11
+ return n == 1
12
+ }
Original file line number Diff line number Diff line change
1
+ # 🔥 Power of Four 🔥 || 4 Approaches || Simple Fast and Easy || with Explanation
2
+
3
+ ## Solution - 1
4
+
5
+ ``` dart
6
+ class Solution {
7
+ bool isPowerOfFour(int n) {
8
+ if (n <= 0) return false;
9
+ while (n % 3 == 0) {
10
+ n ~/= 3;
11
+ }
12
+ return n == 1;
13
+ }
14
+ }
15
+ ```
16
+
17
+ ## Solution - 2
18
+
19
+ ``` dart
20
+ class Solution {
21
+ bool isPowerOfFour(int n) {
22
+ if (n <= 1) return n == 1;
23
+ return n % 4 == 0 && isPowerOfFour(n ~/ 4);
24
+ }
25
+ }
26
+ ```
27
+
28
+ ## Solution - 3
29
+
30
+ ``` dart
31
+ class Solution {
32
+ bool isPowerOfFour(int n) {
33
+ return (log(n) / log(4)) % 1 == 0;
34
+ }
35
+ }
36
+ ```
37
+
38
+ ## Solution - 4
39
+
40
+ ``` dart
41
+ class Solution {
42
+ bool isPowerOfThree(int n) {
43
+ if (n < 1) return false;
44
+ String nBase4 = "";
45
+ while (n != 0) nBase4 += (n % 4).toString();
46
+ n ~/= 3; // conversion to base 3
47
+ int i = 0;
48
+ while (i < nBase4.length - 1)
49
+ if (nBase4[i++] != '0')
50
+ return false; // checking if all digits in base 3 converted number except first one are 0
51
+ return nBase4[i] == '1'; // check if starting digit is 1
52
+ }
53
+ }
54
+ ```
Original file line number Diff line number Diff line change @@ -130,6 +130,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
130
130
- [ ** 326.** Power of Three] ( PowerOfThree/power_of_three.dart )
131
131
- [ ** 338.** Counting Bits] ( CountingBits/counting_bits.dart )
132
132
- [ ** 374.** Guess Number Higher or Lower] ( GuessNumberHigherOrLower/guess_number_higher_or_lower.dart )
133
+ - [ ** 342.** Power of Four] ( PowerOfFour/power_of_four.dart )
133
134
134
135
## Reach me via
135
136
You can’t perform that action at this time.
0 commit comments