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 c9f8c43

Browse files
feat: add solutions to lc problem: No.1780 (doocs#4645)
No.1780.Check if Number is a Sum of Powers of Three
1 parent 7ead208 commit c9f8c43

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

‎solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ tags:
6262

6363
我们发现,如果一个数 $n$ 可以表示成若干个"不同的"三的幂之和,那么 $n$ 的三进制表示中,每一位上的数字只能是 0ドル$ 或者 1ドル$。
6464

65-
因此,我们将 $n$ 转换成三进制,然后判断每一位上的数字是否是 0ドル$ 或者 1ドル$。如果不是,那么 $n$ 就不可以表示成若干个三的幂之和,直接返回 `false`;否则 $n$ 可以表示成若干个三的幂之和,返回 `true`
65+
因此,我们将 $n$ 转换成三进制,然后判断每一位上的数字是否是 0ドル$ 或者 1ドル$。如果不是,那么 $n$ 就不可以表示成若干个三的幂之和,直接返回 $\textit{false}$;否则 $n$ 可以表示成若干个三的幂之和,返回 $\textit{true}$
6666

6767
时间复杂度 $O(\log_3 n),ドル空间复杂度 $O(1)$。
6868

@@ -137,6 +137,39 @@ function checkPowersOfThree(n: number): boolean {
137137
}
138138
```
139139

140+
#### Rust
141+
142+
```rust
143+
impl Solution {
144+
pub fn check_powers_of_three(n: i32) -> bool {
145+
let mut n = n;
146+
while n > 0 {
147+
if n % 3 > 1 {
148+
return false;
149+
}
150+
n /= 3;
151+
}
152+
true
153+
}
154+
}
155+
```
156+
157+
#### C#
158+
159+
```cs
160+
public class Solution {
161+
public bool CheckPowersOfThree(int n) {
162+
while (n > 0) {
163+
if (n % 3 > 1) {
164+
return false;
165+
}
166+
n /= 3;
167+
}
168+
return true;
169+
}
170+
}
171+
```
172+
140173
<!-- tabs:end -->
141174

142175
<!-- solution:end -->

‎solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README_EN.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ tags:
6363

6464
We find that if a number $n$ can be expressed as the sum of several "different" powers of three, then in the ternary representation of $n,ドル each digit can only be 0ドル$ or 1ドル$.
6565

66-
Therefore, we convert $n$ to ternary and then check whether each digit is 0ドル$ or 1ドル$. If not, then $n$ cannot be expressed as the sum of several powers of three, and we directly return `false`; otherwise, $n$ can be expressed as the sum of several powers of three, and we return `true`.
66+
Therefore, we convert $n$ to ternary and then check whether each digit is 0ドル$ or 1ドル$. If not, then $n$ cannot be expressed as the sum of several powers of three, and we directly return $\textit{false}$; otherwise, $n$ can be expressed as the sum of several powers of three, and we return $\textit{true}$.
6767

6868
The time complexity is $O(\log_3 n),ドル and the space complexity is $O(1)$.
6969

@@ -138,6 +138,39 @@ function checkPowersOfThree(n: number): boolean {
138138
}
139139
```
140140

141+
#### Rust
142+
143+
```rust
144+
impl Solution {
145+
pub fn check_powers_of_three(n: i32) -> bool {
146+
let mut n = n;
147+
while n > 0 {
148+
if n % 3 > 1 {
149+
return false;
150+
}
151+
n /= 3;
152+
}
153+
true
154+
}
155+
}
156+
```
157+
158+
#### C#
159+
160+
```cs
161+
public class Solution {
162+
public bool CheckPowersOfThree(int n) {
163+
while (n > 0) {
164+
if (n % 3 > 1) {
165+
return false;
166+
}
167+
n /= 3;
168+
}
169+
return true;
170+
}
171+
}
172+
```
173+
141174
<!-- tabs:end -->
142175

143176
<!-- solution:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Solution {
2+
public bool CheckPowersOfThree(int n) {
3+
while (n > 0) {
4+
if (n % 3 > 1) {
5+
return false;
6+
}
7+
n /= 3;
8+
}
9+
return true;
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn check_powers_of_three(n: i32) -> bool {
3+
let mut n = n;
4+
while n > 0 {
5+
if n % 3 > 1 {
6+
return false;
7+
}
8+
n /= 3;
9+
}
10+
true
11+
}
12+
}

0 commit comments

Comments
(0)

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