Skip to content

Commit 05dd742

Browse files
authored
Merge pull request #1129 from 0xff-dev/main
Add solution and test-cases for problem 1780
2 parents 2c44665 + b90445c commit 05dd742

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1780.Check if Number is a Sum of Powers of Three][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer `n`, return `true` if it is possible to represent `n` as the sum of distinct powers of three. Otherwise, return `false`.
5+
6+
An integer `y` is a power of three if there exists an integer `x` such that `y == 3^x`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: n = 12
12+
Output: true
13+
Explanation: 12 = 31 + 32
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Check if Number is a Sum of Powers of Three
23-
```go
18+
```
19+
Input: n = 91
20+
Output: true
21+
Explanation: 91 = 30 + 32 + 34
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: n = 21
28+
Output: false
29+
```
2630

2731
## 结语
2832

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) bool {
4+
arr := make([]int, 15)
5+
arr[0] = 1
6+
for i := 1; i < 15; i++ {
7+
arr[i] = arr[i-1] * 3
8+
}
9+
var ok func(int, int) bool
10+
ok = func(index, cur int) bool {
11+
if cur == 0 {
12+
return true
13+
}
14+
if index == 15 {
15+
return false
16+
}
17+
18+
return ok(index+1, cur-arr[index]) || ok(index+1, cur)
19+
}
20+
return ok(0, n)
521
}

leetcode/1701-1800/1780.Check-if-Number-is-a-Sum-of-Powers-of-Three/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 12, true},
17+
{"TestCase2", 91, true},
18+
{"TestCase3", 21, false},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)