Skip to content

Add solution and test-cases for problem 1390 #1122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions leetcode/1301-1400/1390.Four-Divisors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# [1390.Four Divisors][title]

## Description
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`.

**Example 1:**

```
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
```

**Example 2:**

```
Input: nums = [21,21]
Output: 64
```

**Example 3:**

```
Input: nums = [1,2,3,4,5]
Output: 0
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/four-divisors
[me]: https://github.com/kylesliu/awesome-golang-algorithm
34 changes: 32 additions & 2 deletions leetcode/1301-1400/1390.Four-Divisors/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package Solution

func Solution(x bool) bool {
return x
import "math"

func check(n int) (bool, int) {
r := 0
ans := 0
for i := 1; i <= int(math.Sqrt(float64(n))); i++ {
if n%i == 0 {
another := n / i
r++
ans += i
if another != i {
r++
ans += another
}
if r > 4 {
return false, 0
}
}
}
return r == 4, ans
}

func Solution(nums []int) int {
ans := 0
for _, n := range nums {
ok, v := check(n)
if !ok {
continue
}
ans += v
}
return ans
}
14 changes: 7 additions & 7 deletions leetcode/1301-1400/1390.Four-Divisors/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{21, 4, 7}, 32},
{"TestCase2", []int{21, 21}, 64},
{"TestCase3", []int{1, 2, 3, 4, 5}, 0},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading