Skip to content

Commit 0a2512f

Browse files
authored
Merge pull request #1122 from 0xff-dev/1390
Add solution and test-cases for problem 1390
2 parents bd6ec8c + 075e957 commit 0a2512f

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [1390.Four Divisors][title]
2+
3+
## Description
4+
Given an integer array `nums`, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return `0`.
5+
6+
**Example 1:**
7+
8+
```
9+
Input: nums = [21,4,7]
10+
Output: 32
11+
Explanation:
12+
21 has 4 divisors: 1, 3, 7, 21
13+
4 has 3 divisors: 1, 2, 4
14+
7 has 2 divisors: 1, 7
15+
The answer is the sum of divisors of 21 only.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: nums = [21,21]
22+
Output: 64
23+
```
24+
25+
**Example 3:**
26+
27+
```
28+
Input: nums = [1,2,3,4,5]
29+
Output: 0
30+
```
31+
32+
## 结语
33+
34+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
35+
36+
[title]: https://leetcode.com/problems/four-divisors
37+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func check(n int) (bool, int) {
6+
r := 0
7+
ans := 0
8+
for i := 1; i <= int(math.Sqrt(float64(n))); i++ {
9+
if n%i == 0 {
10+
another := n / i
11+
r++
12+
ans += i
13+
if another != i {
14+
r++
15+
ans += another
16+
}
17+
if r > 4 {
18+
return false, 0
19+
}
20+
}
21+
}
22+
return r == 4, ans
23+
}
24+
25+
func Solution(nums []int) int {
26+
ans := 0
27+
for _, n := range nums {
28+
ok, v := check(n)
29+
if !ok {
30+
continue
31+
}
32+
ans += v
33+
}
34+
return ans
535
}

leetcode/1301-1400/1390.Four-Divisors/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", []int{21, 4, 7}, 32},
17+
{"TestCase2", []int{21, 21}, 64},
18+
{"TestCase3", []int{1, 2, 3, 4, 5}, 0},
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)