From a505de0723f6797f6b913fb60608a1760f69b972 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 28 Mar 2024 22:56:38 +0800 Subject: [PATCH] Add solution and test-cases for problem 592 --- .../README.md | 28 +++--- .../Solution.go | 90 ++++++++++++++++++- .../Solution_test.go | 14 +-- 3 files changed, 110 insertions(+), 22 deletions(-) diff --git a/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/README.md b/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/README.md index d272d50a7..b1e1d8bad 100644 --- a/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/README.md +++ b/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/README.md @@ -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" +``` ## 结语 diff --git a/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution.go b/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution.go index d115ccf5e..4876aa169 100644 --- a/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution.go +++ b/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution.go @@ -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) } diff --git a/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution_test.go b/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution_test.go index 14ff50eb4..cfe862621 100644 --- a/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution_test.go +++ b/leetcode/501-600/0592.Fraction-Addition-and-Subtraction/Solution_test.go @@ -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"}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }