Skip to content

Commit 06811bc

Browse files
authored
Merge pull request #1355 from 0xff-dev/2177
Add solution and test-cases for problem 2177
2 parents 574212b + cdbbb6e commit 06811bc

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

leetcode/2101-2200/2177.Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
# [2177.Find Three Consecutive Integers That Sum to a Given Number][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 `num`, return three consecutive integers (as a sorted array) that **sum** to `num`. If `num` cannot be expressed as the sum of three consecutive integers, return an **empty** array.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: num = 33
10+
Output: [10,11,12]
11+
Explanation: 33 can be expressed as 10 + 11 + 12 = 33.
12+
10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].
1313
```
1414

15-
## 题意
16-
> ...
17-
18-
## 题解
15+
**Example 2:**
1916

20-
### 思路1
21-
> ...
22-
Find Three Consecutive Integers That Sum to a Given Number
23-
```go
2417
```
25-
18+
Input: num = 4
19+
Output: []
20+
Explanation: There is no way to express 4 as the sum of 3 consecutive integers.
21+
```
2622

2723
## 结语
2824

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(num int64) []int64 {
4+
if num%3 != 0 {
5+
return []int64{}
6+
}
7+
mid := num / 3
8+
return []int64{mid - 1, mid, mid + 1}
59
}

leetcode/2101-2200/2177.Find-Three-Consecutive-Integers-That-Sum-to-a-Given-Number/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int64
14+
expect []int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 33, []int64{10, 11, 12}},
17+
{"TestCase2", 4, []int64{}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)