Skip to content

feat: add solutions to lc problem: No.0326 #4643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions solution/0300-0399/0326.Power of Three/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,29 @@ func isPowerOfThree(n int) bool {

```ts
function isPowerOfThree(n: number): boolean {
return n > 0 && 1162261467 % n == 0;
while (n > 2) {
if (n % 3 !== 0) {
return false;
}
n = Math.floor(n / 3);
}
return n === 1;
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_three(mut n: i32) -> bool {
while n > 2 {
if n % 3 != 0 {
return false;
}
n /= 3;
}
n == 1
}
}
```

Expand All @@ -152,7 +174,13 @@ function isPowerOfThree(n: number): boolean {
* @return {boolean}
*/
var isPowerOfThree = function (n) {
return n > 0 && 1162261467 % n == 0;
while (n > 2) {
if (n % 3 !== 0) {
return false;
}
n = Math.floor(n / 3);
}
return n === 1;
};
```

Expand Down Expand Up @@ -207,6 +235,36 @@ func isPowerOfThree(n int) bool {
}
```

#### TypeScript

```ts
function isPowerOfThree(n: number): boolean {
return n > 0 && 1162261467 % n == 0;
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_three(mut n: i32) -> bool {
n > 0 && 1162261467 % n == 0
}
}
```

#### JavaScript

```js
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfThree = function (n) {
return n > 0 && 1162261467 % n == 0;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
74 changes: 70 additions & 4 deletions solution/0300-0399/0326.Power of Three/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Trial Division

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$.

Time complexity $O(\log_3n)$, space complexity $O(1)$.

<!-- tabs:start -->

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

```ts
function isPowerOfThree(n: number): boolean {
return n > 0 && 1162261467 % n == 0;
while (n > 2) {
if (n % 3 !== 0) {
return false;
}
n = Math.floor(n / 3);
}
return n === 1;
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_three(mut n: i32) -> bool {
while n > 2 {
if n % 3 != 0 {
return false;
}
n /= 3;
}
n == 1
}
}
```

Expand All @@ -141,7 +167,13 @@ function isPowerOfThree(n: number): boolean {
* @return {boolean}
*/
var isPowerOfThree = function (n) {
return n > 0 && 1162261467 % n == 0;
while (n > 2) {
if (n % 3 !== 0) {
return false;
}
n = Math.floor(n / 3);
}
return n === 1;
};
```

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

<!-- solution:start -->

### Solution 2
### Solution 2: Mathematics

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}$.

Time complexity $O(1)$, space complexity $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -192,6 +228,36 @@ func isPowerOfThree(n int) bool {
}
```

#### TypeScript

```ts
function isPowerOfThree(n: number): boolean {
return n > 0 && 1162261467 % n == 0;
}
```

#### Rust

```rust
impl Solution {
pub fn is_power_of_three(mut n: i32) -> bool {
n > 0 && 1162261467 % n == 0
}
}
```

#### JavaScript

```js
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfThree = function (n) {
return n > 0 && 1162261467 % n == 0;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
8 changes: 7 additions & 1 deletion solution/0300-0399/0326.Power of Three/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
* @return {boolean}
*/
var isPowerOfThree = function (n) {
return n > 0 && 1162261467 % n == 0;
while (n > 2) {
if (n % 3 !== 0) {
return false;
}
n = Math.floor(n / 3);
}
return n === 1;
};
11 changes: 11 additions & 0 deletions solution/0300-0399/0326.Power of Three/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl Solution {
pub fn is_power_of_three(mut n: i32) -> bool {
while n > 2 {
if n % 3 != 0 {
return false;
}
n /= 3;
}
n == 1
}
}
8 changes: 7 additions & 1 deletion solution/0300-0399/0326.Power of Three/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function isPowerOfThree(n: number): boolean {
return n > 0 && 1162261467 % n == 0;
while (n > 2) {
if (n % 3 !== 0) {
return false;
}
n = Math.floor(n / 3);
}
return n === 1;
}
7 changes: 7 additions & 0 deletions solution/0300-0399/0326.Power of Three/Solution2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfThree = function (n) {
return n > 0 && 1162261467 % n == 0;
};
5 changes: 5 additions & 0 deletions solution/0300-0399/0326.Power of Three/Solution2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn is_power_of_three(mut n: i32) -> bool {
n > 0 && 1162261467 % n == 0
}
}
3 changes: 3 additions & 0 deletions solution/0300-0399/0326.Power of Three/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function isPowerOfThree(n: number): boolean {
return n > 0 && 1162261467 % n == 0;
}