Skip to content

Commit 0b3bb25

Browse files
committed
feat: add solutions to lc problem: No.1780
No.1780.Check if Number is a Sum of Powers of Three
1 parent 4aa944b commit 0b3bb25

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,75 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:数学分析**
48+
49+
我们发现,如果一个数 $n$ 可以表示成若干个三的幂之和,那么 $n$ 的三进制表示中,每一位上的数字只能是 $0$ 或者 $1$。
50+
51+
因此,我们将 $n$ 转换成三进制,然后判断每一位上的数字是否是 $0$ 或者 $1$。如果不是,那么 $n$ 就不可以表示成若干个三的幂之和,直接返回 `false`;否则 $n$ 可以表示成若干个三的幂之和,返回 `true`
52+
53+
时间复杂度 $O(\log_3 n)$,空间复杂度 $O(1)$。
54+
4755
<!-- tabs:start -->
4856

4957
### **Python3**
5058

5159
<!-- 这里可写当前语言的特殊实现逻辑 -->
5260

5361
```python
54-
62+
class Solution:
63+
def checkPowersOfThree(self, n: int) -> bool:
64+
while n:
65+
if n % 3 > 1:
66+
return False
67+
n //= 3
68+
return True
5569
```
5670

5771
### **Java**
5872

5973
<!-- 这里可写当前语言的特殊实现逻辑 -->
6074

6175
```java
76+
class Solution {
77+
public boolean checkPowersOfThree(int n) {
78+
while (n > 0) {
79+
if (n % 3 > 1) {
80+
return false;
81+
}
82+
n /= 3;
83+
}
84+
return true;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
bool checkPowersOfThree(int n) {
95+
while (n) {
96+
if (n % 3 > 1) return false;
97+
n /= 3;
98+
}
99+
return true;
100+
}
101+
};
102+
```
62103
104+
### **Go**
105+
106+
```go
107+
func checkPowersOfThree(n int) bool {
108+
for n > 0 {
109+
if n%3 > 1 {
110+
return false
111+
}
112+
n /= 3
113+
}
114+
return true
115+
}
63116
```
64117

65118
### **...**

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,58 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def checkPowersOfThree(self, n: int) -> bool:
51+
while n:
52+
if n % 3 > 1:
53+
return False
54+
n //= 3
55+
return True
5056
```
5157

5258
### **Java**
5359

5460
```java
61+
class Solution {
62+
public boolean checkPowersOfThree(int n) {
63+
while (n > 0) {
64+
if (n % 3 > 1) {
65+
return false;
66+
}
67+
n /= 3;
68+
}
69+
return true;
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
bool checkPowersOfThree(int n) {
80+
while (n) {
81+
if (n % 3 > 1) return false;
82+
n /= 3;
83+
}
84+
return true;
85+
}
86+
};
87+
```
5588
89+
### **Go**
90+
91+
```go
92+
func checkPowersOfThree(n int) bool {
93+
for n > 0 {
94+
if n%3 > 1 {
95+
return false
96+
}
97+
n /= 3
98+
}
99+
return true
100+
}
56101
```
57102

58103
### **...**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
bool checkPowersOfThree(int n) {
4+
while (n) {
5+
if (n % 3 > 1) return false;
6+
n /= 3;
7+
}
8+
return true;
9+
}
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func checkPowersOfThree(n int) bool {
2+
for n > 0 {
3+
if n%3 > 1 {
4+
return false
5+
}
6+
n /= 3
7+
}
8+
return true
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def checkPowersOfThree(self, n: int) -> bool:
3+
while n:
4+
if n % 3 > 1:
5+
return False
6+
n //= 3
7+
return True

0 commit comments

Comments
 (0)