Skip to content

Commit 8fb0717

Browse files
committed
Add solution and test-cases for problem 1447
1 parent a01e89c commit 8fb0717

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [1447.Simplified Fractions][title]
2+
3+
## Description
4+
Given an integer `n`, return a list of all **simplified** fractions between `0` and `1` (exclusive) such that the denominator is less-than-or-equal-to `n`. You can return the answer in **any order**.
5+
6+
**Example 1:**
7+
8+
```
9+
Input: n = 2
10+
Output: ["1/2"]
11+
Explanation: "1/2" is the only unique fraction with a denominator less-than-or-equal-to 2.
12+
```
13+
14+
**Example 2:**
15+
16+
```
17+
Input: n = 3
18+
Output: ["1/2","1/3","2/3"]
19+
```
20+
21+
**Example 3:**
22+
23+
```
24+
Input: n = 4
25+
Output: ["1/2","1/3","1/4","2/3","3/4"]
26+
Explanation: "2/4" is not a simplified fraction because it can be simplified to "1/2".
27+
```
28+
29+
## 结语
30+
31+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
32+
33+
[title]: https://leetcode.com/problems/simplified-fractions
34+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "fmt"
4+
5+
func gcd(a, b int) int {
6+
if b == 0 {
7+
return a
8+
}
9+
return gcd(b, a%b)
10+
}
11+
12+
func Solution(n int) []string {
13+
in := make(map[string]struct{})
14+
for i := 2; i <= n; i++ {
15+
for j := 1; j < i; j++ {
16+
m := gcd(j, i)
17+
in[fmt.Sprintf("%d/%d", j/m, i/m)] = struct{}{}
18+
}
19+
}
20+
ans := make([]string, 0)
21+
for i := range in {
22+
ans = append(ans, i)
23+
}
24+
return ans
525
}

leetcode/1401-1500/1447.Simplified-Fractions/Solution_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package Solution
22

33
import (
44
"reflect"
5+
"sort"
56
"strconv"
67
"testing"
78
)
@@ -10,18 +11,19 @@ func TestSolution(t *testing.T) {
1011
// 测试用例
1112
cases := []struct {
1213
name string
13-
inputs bool
14-
expect bool
14+
inputs int
15+
expect []string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 2, []string{"1/2"}},
18+
{"TestCase2", 3, []string{"1/2", "1/3", "2/3"}},
19+
{"TestCase3", 4, []string{"1/2", "1/3", "1/4", "2/3", "3/4"}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
2425
got := Solution(c.inputs)
26+
sort.Strings(got)
2527
if !reflect.DeepEqual(got, c.expect) {
2628
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2729
c.expect, got, c.inputs)
@@ -30,10 +32,10 @@ func TestSolution(t *testing.T) {
3032
}
3133
}
3234

33-
// 压力测试
35+
// 压力测试
3436
func BenchmarkSolution(b *testing.B) {
3537
}
3638

37-
// 使用案列
39+
// 使用案列
3840
func ExampleSolution() {
3941
}

0 commit comments

Comments
 (0)