diff --git a/leetcode/1901-2000/1952.Three-Divisors/README.md b/leetcode/1901-2000/1952.Three-Divisors/README.md index b2fe7eac9..d85dde577 100755 --- a/leetcode/1901-2000/1952.Three-Divisors/README.md +++ b/leetcode/1901-2000/1952.Three-Divisors/README.md @@ -1,28 +1,25 @@ # [1952.Three Divisors][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given an integer `n`, return `true` if n has **exactly three positive divisors**. Otherwise, return `false`. + +An integer m is a divisor of n if there exists an integer k such that n = k * m. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 2 +Output: false +Explantion: 2 has only two divisors: 1 and 2. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Three Divisors -```go ``` - +Input: n = 4 +Output: true +Explantion: 4 has three divisors: 1, 2, and 4. +``` ## 结语 diff --git a/leetcode/1901-2000/1952.Three-Divisors/Solution.go b/leetcode/1901-2000/1952.Three-Divisors/Solution.go index d115ccf5e..6b0619d9a 100644 --- a/leetcode/1901-2000/1952.Three-Divisors/Solution.go +++ b/leetcode/1901-2000/1952.Three-Divisors/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) bool { + c := 0 + for i := 2; i < n; i++ { + if n%i == 0 { + r := n / i + if r != i { + return false + } + c++ + if c > 1 { + return false + } + } + } + return c == 1 } diff --git a/leetcode/1901-2000/1952.Three-Divisors/Solution_test.go b/leetcode/1901-2000/1952.Three-Divisors/Solution_test.go index 14ff50eb4..11a52a4c7 100644 --- a/leetcode/1901-2000/1952.Three-Divisors/Solution_test.go +++ b/leetcode/1901-2000/1952.Three-Divisors/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 2, false}, + {"TestCase2", 4, true}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }