Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 592 #825

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions leetcode/501-600/0592.Fraction-Addition-and-Subtraction/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# [592.Fraction Addition and Subtraction][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 a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.

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`.

**Example 1:**

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

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Fraction Addition and Subtraction
```go
```
Input: expression = "-1/2+1/2+1/3"
Output: "1/3"
```

**Example 3:**

```
Input: expression = "1/3-1/2"
Output: "-1/6"
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,91 @@
package Solution

func Solution(x bool) bool {
return x
import "fmt"

func gcd592(a, b int) int {
for b != 0 {
a, b = b, a%b
}
return a
}

func add592(a, b, c, d int) (int, int) {
denominator := b * d
numerator := a*d + b*c
lessZero := false
if numerator < 0 {
lessZero = true
numerator = -numerator
}
g := gcd592(numerator, denominator)
denominator /= g
numerator /= g
if lessZero {
numerator = -numerator
}

return numerator, denominator
}
func sub592(a, b, c, d int) (int, int) {

denominator := b * d
numerator := a*d - b*c
lessZero := false
if numerator < 0 {
lessZero = true
numerator = -numerator
}
g := gcd592(numerator, denominator)
denominator /= g
numerator /= g
if lessZero {
numerator = -numerator
}

return numerator, denominator
}

func Solution(expression string) string {
numerator, denominator := 0, 1
nums := 0
op := byte('+')
if expression[0] != '-' {
nums++
}
n, d := 0, 0
f := true
for index := 0; index < len(expression); index++ {
if expression[index] == '+' || expression[index] == '-' {
nums++
if nums == 2 {
if op == '+' {
numerator, denominator = add592(numerator, denominator, n, d)
} else {
numerator, denominator = sub592(numerator, denominator, n, d)
}
nums = 1
}

op = expression[index]
f = true
n, d = 0, 0
continue
}
if expression[index] == '/' {
f = false
}
if expression[index] >= '0' && expression[index] <= '9' {
if f {
n = n*10 + int(expression[index]-'0')
} else {
d = d*10 + int(expression[index]-'0')
}
}
}
if op == '+' {
numerator, denominator = add592(numerator, denominator, n, d)
} else {
numerator, denominator = sub592(numerator, denominator, n, d)
}
return fmt.Sprintf("%d/%d", numerator, denominator)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "-1/2+1/2", "0/1"},
{"TestCase2", "-1/2+1/2+1/3", "1/3"},
{"TestCase3", "1/3-1/2", "-1/6"},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading