Skip to content

Commit b184f6a

Browse files
committed
Add solution and test-cases for problem 1952
1 parent a01e89c commit b184f6a

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

leetcode/1901-2000/1952.Three-Divisors/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [1952.Three Divisors][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer `n`, return `true` if n has **exactly three positive divisors**. Otherwise, return `false`.
5+
6+
An integer m is a divisor of n if there exists an integer k such that n = k * m.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: n = 2
12+
Output: false
13+
Explantion: 2 has only two divisors: 1 and 2.
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Three Divisors
23-
```go
2418
```
25-
19+
Input: n = 4
20+
Output: true
21+
Explantion: 4 has three divisors: 1, 2, and 4.
22+
```
2623

2724
## 结语
2825

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) bool {
4+
c := 0
5+
for i := 2; i < n; i++ {
6+
if n%i == 0 {
7+
r := n / i
8+
if r != i {
9+
return false
10+
}
11+
c++
12+
if c > 1 {
13+
return false
14+
}
15+
}
16+
}
17+
return c == 1
518
}

leetcode/1901-2000/1952.Three-Divisors/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 2, false},
17+
{"TestCase2", 4, true},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)