From 075e957b368676270299dd3f1aa7dbfe91130c54 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 14 Feb 2025 09:14:28 +0800 Subject: [PATCH] Add solution and test-cases for problem 1390 --- .../1301-1400/1390.Four-Divisors/README.md | 37 +++++++++++++++++++ .../1301-1400/1390.Four-Divisors/Solution.go | 34 ++++++++++++++++- .../1390.Four-Divisors/Solution_test.go | 14 +++---- 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 leetcode/1301-1400/1390.Four-Divisors/README.md diff --git a/leetcode/1301-1400/1390.Four-Divisors/README.md b/leetcode/1301-1400/1390.Four-Divisors/README.md new file mode 100644 index 000000000..55b02629c --- /dev/null +++ b/leetcode/1301-1400/1390.Four-Divisors/README.md @@ -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 diff --git a/leetcode/1301-1400/1390.Four-Divisors/Solution.go b/leetcode/1301-1400/1390.Four-Divisors/Solution.go index d115ccf5e..7ac98f811 100755 --- a/leetcode/1301-1400/1390.Four-Divisors/Solution.go +++ b/leetcode/1301-1400/1390.Four-Divisors/Solution.go @@ -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 } diff --git a/leetcode/1301-1400/1390.Four-Divisors/Solution_test.go b/leetcode/1301-1400/1390.Four-Divisors/Solution_test.go index 14ff50eb4..889a0bf34 100755 --- a/leetcode/1301-1400/1390.Four-Divisors/Solution_test.go +++ b/leetcode/1301-1400/1390.Four-Divisors/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }