Skip to content

Commit ab66aa7

Browse files
authored
Merge pull request #825 from 0xff-dev/592
Add solution and test-cases for problem 592
2 parents 64cb4fd + a505de0 commit ab66aa7

File tree

3 files changed

+110
-22
lines changed

3 files changed

+110
-22
lines changed

leetcode/501-600/0592.Fraction-Addition-and-Subtraction/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [592.Fraction Addition and Subtraction][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 a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
5+
6+
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fraction that has a denominator `1`. So in this case, `2` should be converted to `2/1`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: expression = "-1/2+1/2"
12+
Output: "0/1"
1313
```
1414

15-
## 题意
16-
> ...
15+
**Example 2:**
1716

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Fraction Addition and Subtraction
23-
```go
17+
```
18+
Input: expression = "-1/2+1/2+1/3"
19+
Output: "1/3"
2420
```
2521

22+
**Example 3:**
23+
24+
```
25+
Input: expression = "1/3-1/2"
26+
Output: "-1/6"
27+
```
2628

2729
## 结语
2830

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "fmt"
4+
5+
func gcd592(a, b int) int {
6+
for b != 0 {
7+
a, b = b, a%b
8+
}
9+
return a
10+
}
11+
12+
func add592(a, b, c, d int) (int, int) {
13+
denominator := b * d
14+
numerator := a*d + b*c
15+
lessZero := false
16+
if numerator < 0 {
17+
lessZero = true
18+
numerator = -numerator
19+
}
20+
g := gcd592(numerator, denominator)
21+
denominator /= g
22+
numerator /= g
23+
if lessZero {
24+
numerator = -numerator
25+
}
26+
27+
return numerator, denominator
28+
}
29+
func sub592(a, b, c, d int) (int, int) {
30+
31+
denominator := b * d
32+
numerator := a*d - b*c
33+
lessZero := false
34+
if numerator < 0 {
35+
lessZero = true
36+
numerator = -numerator
37+
}
38+
g := gcd592(numerator, denominator)
39+
denominator /= g
40+
numerator /= g
41+
if lessZero {
42+
numerator = -numerator
43+
}
44+
45+
return numerator, denominator
46+
}
47+
48+
func Solution(expression string) string {
49+
numerator, denominator := 0, 1
50+
nums := 0
51+
op := byte('+')
52+
if expression[0] != '-' {
53+
nums++
54+
}
55+
n, d := 0, 0
56+
f := true
57+
for index := 0; index < len(expression); index++ {
58+
if expression[index] == '+' || expression[index] == '-' {
59+
nums++
60+
if nums == 2 {
61+
if op == '+' {
62+
numerator, denominator = add592(numerator, denominator, n, d)
63+
} else {
64+
numerator, denominator = sub592(numerator, denominator, n, d)
65+
}
66+
nums = 1
67+
}
68+
69+
op = expression[index]
70+
f = true
71+
n, d = 0, 0
72+
continue
73+
}
74+
if expression[index] == '/' {
75+
f = false
76+
}
77+
if expression[index] >= '0' && expression[index] <= '9' {
78+
if f {
79+
n = n*10 + int(expression[index]-'0')
80+
} else {
81+
d = d*10 + int(expression[index]-'0')
82+
}
83+
}
84+
}
85+
if op == '+' {
86+
numerator, denominator = add592(numerator, denominator, n, d)
87+
} else {
88+
numerator, denominator = sub592(numerator, denominator, n, d)
89+
}
90+
return fmt.Sprintf("%d/%d", numerator, denominator)
591
}

leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "-1/2+1/2", "0/1"},
17+
{"TestCase2", "-1/2+1/2+1/3", "1/3"},
18+
{"TestCase3", "1/3-1/2", "-1/6"},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)