Skip to content

Commit 85169c1

Browse files
committed
leetcode
1 parent c0b812d commit 85169c1

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

PowerOfThree/power_of_three.dart

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
3+
-* 326. Power of Three *-
4+
5+
6+
Given an integer n, return true if it is a power of three. Otherwise, return false.
7+
8+
An integer n is a power of three, if there exists an integer x such that n == 3x.
9+
10+
11+
12+
Example 1:
13+
14+
Input: n = 27
15+
Output: true
16+
Explanation: 27 = 33
17+
Example 2:
18+
19+
Input: n = 0
20+
Output: false
21+
Explanation: There is no x where 3x = 0.
22+
Example 3:
23+
24+
Input: n = -1
25+
Output: false
26+
Explanation: There is no x where 3x = (-1).
27+
28+
29+
Constraints:
30+
31+
-231 <= n <= 231 - 1
32+
33+
34+
Follow up: Could you solve it without loops/recursion?
35+
36+
37+
*/
38+
39+
import 'dart:collection';
40+
import 'dart:math';
41+
42+
class A {
43+
bool isPowerOfThree(int n) {
44+
if (n <= 0) return false;
45+
while (n % 3 == 0) {
46+
n ~/= 3;
47+
}
48+
return n == 1;
49+
}
50+
}
51+
52+
class B {
53+
bool isPowerOfThree(int n) {
54+
return (log(n) / log(3)) % 1 == 0;
55+
}
56+
}
57+
58+
class C {
59+
bool isPowerOfThree(int n) {
60+
HashMap<int, bool> m = HashMap();
61+
m[1] = true;
62+
m[3] = true;
63+
m[9] = true;
64+
m[27] = true;
65+
m[81] = true;
66+
m[243] = true;
67+
m[729] = true;
68+
m[2187] = true;
69+
m[6561] = true;
70+
m[19683] = true;
71+
m[59049] = true;
72+
m[177147] = true;
73+
m[531441] = true;
74+
m[1594323] = true;
75+
m[4782969] = true;
76+
m[14348907] = true;
77+
m[43046721] = true;
78+
m[129140163] = true;
79+
m[387420489] = true;
80+
m[1162261467] = true;
81+
return m[n] ?? false;
82+
}
83+
}
84+
85+
class D {
86+
bool isPowerOfThree(int n) {
87+
if (n <= 1) return n == 1;
88+
return n % 3 == 0 && isPowerOfThree(n ~/ 3);
89+
}
90+
}
91+
92+
class E {
93+
bool isPowerOfThree(int n) {
94+
if (n < 1) return false;
95+
for (; n != 1; n ~/= 3) if (n % 3 != 0) return false;
96+
return true;
97+
}
98+
}
99+
100+
class F {
101+
bool isPowerOfThree(int n) {
102+
return n > 0 && 1162261467 % n == 0;
103+
// pow(3, floor(log(INT_MAX)/log(3))) = 1162261467
104+
}
105+
}
106+
107+
class G {
108+
bool isPowerOfThree(int n) {
109+
if (n < 1) return false;
110+
String nBase3 = "";
111+
while (n != 0) nBase3 += (n % 3).toString();
112+
n ~/= 3; // conversion to base 3
113+
int i = 0;
114+
while (i < nBase3.length - 1)
115+
if (nBase3[i++] != '0')
116+
return false; // checking if all digits in base 3 converted number except first one are 0
117+
return nBase3[i] == '1'; // check if starting digit is 1
118+
}
119+
}

PowerOfThree/power_of_three.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
func isPowerOfThree(n int) bool {
4+
if n <= 0 {
5+
return false
6+
}
7+
for n%3 == 0 {
8+
n /= 3
9+
}
10+
return n == 1
11+
}

PowerOfThree/power_of_three.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# 🔥 Power of Three 🔥 || 7 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
bool isPowerOfThree(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 isPowerOfThree(int n) {
22+
return (log(n) / log(3)) % 1 == 0;
23+
}
24+
}
25+
```
26+
27+
## Solution - 3
28+
29+
```dart
30+
import 'dart:collection';
31+
32+
class Solution {
33+
bool isPowerOfThree(int n) {
34+
HashMap<int, bool> m = HashMap();
35+
m[1] = true;
36+
m[3] = true;
37+
m[9] = true;
38+
m[27] = true;
39+
m[81] = true;
40+
m[243] = true;
41+
m[729] = true;
42+
m[2187] = true;
43+
m[6561] = true;
44+
m[19683] = true;
45+
m[59049] = true;
46+
m[177147] = true;
47+
m[531441] = true;
48+
m[1594323] = true;
49+
m[4782969] = true;
50+
m[14348907] = true;
51+
m[43046721] = true;
52+
m[129140163] = true;
53+
m[387420489] = true;
54+
m[1162261467] = true;
55+
return m[n] ?? false;
56+
}
57+
}
58+
```
59+
60+
## Solution - 4
61+
62+
```dart
63+
class Solution {
64+
bool isPowerOfThree(int n) {
65+
if (n <= 1) return n == 1;
66+
return n % 3 == 0 && isPowerOfThree(n ~/ 3);
67+
}
68+
}
69+
```
70+
71+
## Solution - 5
72+
73+
```dart
74+
class Solution {
75+
bool isPowerOfThree(int n) {
76+
if (n < 1) return false;
77+
for (; n != 1; n ~/= 3) if (n % 3 != 0) return false;
78+
return true;
79+
}
80+
}
81+
```
82+
83+
## Solution - 6
84+
85+
```dart
86+
class Solution {
87+
bool isPowerOfThree(int n) {
88+
return n > 0 && 1162261467 % n == 0;
89+
// pow(3, floor(log(INT_MAX)/log(3))) = 1162261467
90+
}
91+
}
92+
```
93+
94+
## Solution - 7
95+
96+
```dart
97+
class Solution {
98+
bool isPowerOfThree(int n) {
99+
if (n < 1) return false;
100+
String nBase3 = "";
101+
while (n != 0) nBase3 += (n % 3).toString();
102+
n ~/= 3; // conversion to base 3
103+
int i = 0;
104+
while (i < nBase3.length - 1)
105+
if (nBase3[i++] != '0')
106+
return false; // checking if all digits in base 3 converted number except first one are 0
107+
return nBase3[i] == '1'; // check if starting digit is 1
108+
}
109+
}
110+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
127127
- [**151.** Reverse Words in a String](ReverseWordsInAString/reverse_words_in_a_string.dart)
128128
- [**947.** Most Stones Removed with Same Row or Column](MostStonesRemovedWithSameRowOrColumn/most_stones_removed_with_same_row_or_column.dart)
129129
- [**222.** Count Complete Tree Nodes](CountCompleteTreeNodes/count_complete_tree_nodes.dart)
130+
- [**326.** Power of Three](PowerOfThree/power_of_three.dart)
130131

131132
## Reach me via
132133

0 commit comments

Comments
 (0)