Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b0c8ad

Browse files
feat: add solutions to lc problem: No.0326 (doocs#4643)
No.0326.Power of Three
1 parent 411eafb commit 9b0c8ad

File tree

8 files changed

+170
-8
lines changed

8 files changed

+170
-8
lines changed

‎solution/0300-0399/0326.Power of Three/README.md‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,29 @@ func isPowerOfThree(n int) bool {
140140

141141
```ts
142142
function isPowerOfThree(n: number): boolean {
143-
return n > 0 && 1162261467 % n == 0;
143+
while (n > 2) {
144+
if (n % 3 !== 0) {
145+
return false;
146+
}
147+
n = Math.floor(n / 3);
148+
}
149+
return n === 1;
150+
}
151+
```
152+
153+
#### Rust
154+
155+
```rust
156+
impl Solution {
157+
pub fn is_power_of_three(mut n: i32) -> bool {
158+
while n > 2 {
159+
if n % 3 != 0 {
160+
return false;
161+
}
162+
n /= 3;
163+
}
164+
n == 1
165+
}
144166
}
145167
```
146168

@@ -152,7 +174,13 @@ function isPowerOfThree(n: number): boolean {
152174
* @return {boolean}
153175
*/
154176
var isPowerOfThree = function (n) {
155-
return n > 0 && 1162261467 % n == 0;
177+
while (n > 2) {
178+
if (n % 3 !== 0) {
179+
return false;
180+
}
181+
n = Math.floor(n / 3);
182+
}
183+
return n === 1;
156184
};
157185
```
158186

@@ -207,6 +235,36 @@ func isPowerOfThree(n int) bool {
207235
}
208236
```
209237

238+
#### TypeScript
239+
240+
```ts
241+
function isPowerOfThree(n: number): boolean {
242+
return n > 0 && 1162261467 % n == 0;
243+
}
244+
```
245+
246+
#### Rust
247+
248+
```rust
249+
impl Solution {
250+
pub fn is_power_of_three(mut n: i32) -> bool {
251+
n > 0 && 1162261467 % n == 0
252+
}
253+
}
254+
```
255+
256+
#### JavaScript
257+
258+
```js
259+
/**
260+
* @param {number} n
261+
* @return {boolean}
262+
*/
263+
var isPowerOfThree = function (n) {
264+
return n > 0 && 1162261467 % n == 0;
265+
};
266+
```
267+
210268
<!-- tabs:end -->
211269

212270
<!-- solution:end -->

‎solution/0300-0399/0326.Power of Three/README_EN.md‎

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Trial Division
66+
67+
If $n \gt 2,ドル we can continuously divide $n$ by 3ドル$. If it's not divisible, it means $n$ is not a power of 3ドル,ドル otherwise we continue dividing by 3ドル$ until $n$ is less than or equal to 2ドル$. If $n$ equals 1ドル,ドル it means $n$ is a power of 3ドル,ドル otherwise it's not a power of 3ドル$.
68+
69+
Time complexity $O(\log_3n),ドル space complexity $O(1)$.
6670

6771
<!-- tabs:start -->
6872

@@ -129,7 +133,29 @@ func isPowerOfThree(n int) bool {
129133

130134
```ts
131135
function isPowerOfThree(n: number): boolean {
132-
return n > 0 && 1162261467 % n == 0;
136+
while (n > 2) {
137+
if (n % 3 !== 0) {
138+
return false;
139+
}
140+
n = Math.floor(n / 3);
141+
}
142+
return n === 1;
143+
}
144+
```
145+
146+
#### Rust
147+
148+
```rust
149+
impl Solution {
150+
pub fn is_power_of_three(mut n: i32) -> bool {
151+
while n > 2 {
152+
if n % 3 != 0 {
153+
return false;
154+
}
155+
n /= 3;
156+
}
157+
n == 1
158+
}
133159
}
134160
```
135161

@@ -141,7 +167,13 @@ function isPowerOfThree(n: number): boolean {
141167
* @return {boolean}
142168
*/
143169
var isPowerOfThree = function (n) {
144-
return n > 0 && 1162261467 % n == 0;
170+
while (n > 2) {
171+
if (n % 3 !== 0) {
172+
return false;
173+
}
174+
n = Math.floor(n / 3);
175+
}
176+
return n === 1;
145177
};
146178
```
147179

@@ -151,7 +183,11 @@ var isPowerOfThree = function (n) {
151183

152184
<!-- solution:start -->
153185

154-
### Solution 2
186+
### Solution 2: Mathematics
187+
188+
If $n$ is a power of 3ドル,ドル then the maximum value of $n$ is 3ドル^{19} = 1162261467$. Therefore, we only need to check if $n$ is a divisor of 3ドル^{19}$.
189+
190+
Time complexity $O(1),ドル space complexity $O(1)$.
155191

156192
<!-- tabs:start -->
157193

@@ -192,6 +228,36 @@ func isPowerOfThree(n int) bool {
192228
}
193229
```
194230

231+
#### TypeScript
232+
233+
```ts
234+
function isPowerOfThree(n: number): boolean {
235+
return n > 0 && 1162261467 % n == 0;
236+
}
237+
```
238+
239+
#### Rust
240+
241+
```rust
242+
impl Solution {
243+
pub fn is_power_of_three(mut n: i32) -> bool {
244+
n > 0 && 1162261467 % n == 0
245+
}
246+
}
247+
```
248+
249+
#### JavaScript
250+
251+
```js
252+
/**
253+
* @param {number} n
254+
* @return {boolean}
255+
*/
256+
var isPowerOfThree = function (n) {
257+
return n > 0 && 1162261467 % n == 0;
258+
};
259+
```
260+
195261
<!-- tabs:end -->
196262

197263
<!-- solution:end -->

‎solution/0300-0399/0326.Power of Three/Solution.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
* @return {boolean}
44
*/
55
var isPowerOfThree = function (n) {
6-
return n > 0 && 1162261467 % n == 0;
6+
while (n > 2) {
7+
if (n % 3 !== 0) {
8+
return false;
9+
}
10+
n = Math.floor(n / 3);
11+
}
12+
return n === 1;
713
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn is_power_of_three(mut n: i32) -> bool {
3+
while n > 2 {
4+
if n % 3 != 0 {
5+
return false;
6+
}
7+
n /= 3;
8+
}
9+
n == 1
10+
}
11+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
function isPowerOfThree(n: number): boolean {
2-
return n > 0 && 1162261467 % n == 0;
2+
while (n > 2) {
3+
if (n % 3 !== 0) {
4+
return false;
5+
}
6+
n = Math.floor(n / 3);
7+
}
8+
return n === 1;
39
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
var isPowerOfThree = function (n) {
6+
return n > 0 && 1162261467 % n == 0;
7+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn is_power_of_three(mut n: i32) -> bool {
3+
n > 0 && 1162261467 % n == 0
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function isPowerOfThree(n: number): boolean {
2+
return n > 0 && 1162261467 % n == 0;
3+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /