Skip to content

Commit b2aacc5

Browse files
committed
Add solution and test-cases for problem 2652
1 parent df4288a commit b2aacc5

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [2652.Sum Multiples][title]
2+
3+
## Description
4+
Given a positive integer `n`, find the sum of all integers in the range `[1, n]` **inclusive** that are divisible by `3`, `5`, or `7`.
5+
6+
Return an integer denoting the sum of all numbers in the given range satisfying the constraint.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: n = 7
12+
Output: 21
13+
Explanation: Numbers in the range [1, 7] that are divisible by 3, 5, or 7 are 3, 5, 6, 7. The sum of these numbers is 21.
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: n = 10
20+
Output: 40
21+
Explanation: Numbers in the range [1, 10] that are divisible by 3, 5, or 7 are 3, 5, 6, 7, 9, 10. The sum of these numbers is 40.
22+
```
23+
24+
**Example 3:**
25+
26+
```
27+
Input: n = 9
28+
Output: 30
29+
Explanation: Numbers in the range [1, 9] that are divisible by 3, 5, or 7 are 3, 5, 6, 7, 9. The sum of these numbers is 30.
30+
```
31+
32+
## 结语
33+
34+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
35+
36+
[title]: https://leetcode.com/problems/sum-multiples
37+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) int {
4+
sum := 0
5+
for i := 1; i <= n; i++ {
6+
if i%3 == 0 || i%5 == 0 || i%7 == 0 {
7+
sum += i
8+
}
9+
}
10+
return sum
511
}

leetcode/2601-2700/2652.Sum-Multiples/Solution_test.go

Lines changed: 7 additions & 7 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
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 7, 21},
17+
{"TestCase2", 9, 30},
18+
{"TestCase3", 10, 40},
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)