Skip to content

Commit 853a54d

Browse files
committed
feat: add solutions to lc problem: No.0592
No.0592.Fraction Addition and Subtraction
1 parent 919c7ca commit 853a54d

File tree

5 files changed

+286
-2
lines changed

5 files changed

+286
-2
lines changed

solution/0500-0599/0592.Fraction Addition and Subtraction/README.md

+98-1
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,119 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:数学**
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
<!-- 这里可写当前语言的特殊实现逻辑 -->
5759

5860
```python
59-
61+
class Solution:
62+
def fractionAddition(self, expression: str) -> str:
63+
x, y = 0, 6 * 7 * 8 * 9 * 10
64+
if expression[0].isdigit():
65+
expression = '+' + expression
66+
i, n = 0, len(expression)
67+
while i < n:
68+
sign = -1 if expression[i] == '-' else 1
69+
i += 1
70+
j = i
71+
while j < n and expression[j] not in '+-':
72+
j += 1
73+
s = expression[i: j]
74+
a, b = s.split('/')
75+
x += sign * int(a) * y // int(b)
76+
i = j
77+
z = gcd(x, y)
78+
x //= z
79+
y //= z
80+
return f'{x}/{y}'
6081
```
6182

6283
### **Java**
6384

6485
<!-- 这里可写当前语言的特殊实现逻辑 -->
6586

6687
```java
88+
class Solution {
89+
public String fractionAddition(String expression) {
90+
int x = 0, y = 6 * 7 * 8 * 9 * 10;
91+
if (Character.isDigit(expression.charAt(0))) {
92+
expression = "+" + expression;
93+
}
94+
int i = 0, n = expression.length();
95+
while (i < n) {
96+
int sign = expression.charAt(i) == '-' ? -1 : 1;
97+
++i;
98+
int j = i;
99+
while (j < n && expression.charAt(j) != '+' && expression.charAt(j) != '-') {
100+
++j;
101+
}
102+
String s = expression.substring(i, j);
103+
String[] t = s.split("/");
104+
int a = Integer.parseInt(t[0]), b = Integer.parseInt(t[1]);
105+
x += sign * a * y / b;
106+
i = j;
107+
}
108+
int z = gcd(Math.abs(x), y);
109+
x /= z;
110+
y /= z;
111+
return x + "/" + y;
112+
}
113+
114+
private int gcd(int a, int b) {
115+
return b == 0 ? a : gcd(b, a % b);
116+
}
117+
}
118+
```
67119

120+
### **Go**
121+
122+
```go
123+
func fractionAddition(expression string) string {
124+
x, y := 0, 6*7*8*9*10
125+
if unicode.IsDigit(rune(expression[0])) {
126+
expression = "+" + expression
127+
}
128+
i, n := 0, len(expression)
129+
for i < n {
130+
sign := 1
131+
if expression[i] == '-' {
132+
sign = -1
133+
}
134+
i++
135+
j := i
136+
for j < n && expression[j] != '+' && expression[j] != '-' {
137+
j++
138+
}
139+
s := expression[i:j]
140+
t := strings.Split(s, "/")
141+
a, _ := strconv.Atoi(t[0])
142+
b, _ := strconv.Atoi(t[1])
143+
x += sign * a * y / b
144+
i = j
145+
}
146+
z := gcd(abs(x), y)
147+
x /= z
148+
y /= z
149+
return fmt.Sprintf("%d/%d", x, y)
150+
}
151+
152+
func abs(x int) int {
153+
if x < 0 {
154+
return -x
155+
}
156+
return x
157+
}
158+
159+
func gcd(a, b int) int {
160+
if b == 0 {
161+
return a
162+
}
163+
return gcd(b, a%b)
164+
}
68165
```
69166

70167
### **...**

solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,108 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def fractionAddition(self, expression: str) -> str:
53+
x, y = 0, 6 * 7 * 8 * 9 * 10
54+
if expression[0].isdigit():
55+
expression = '+' + expression
56+
i, n = 0, len(expression)
57+
while i < n:
58+
sign = -1 if expression[i] == '-' else 1
59+
i += 1
60+
j = i
61+
while j < n and expression[j] not in '+-':
62+
j += 1
63+
s = expression[i: j]
64+
a, b = s.split('/')
65+
x += sign * int(a) * y // int(b)
66+
i = j
67+
z = gcd(x, y)
68+
x //= z
69+
y //= z
70+
return f'{x}/{y}'
5271
```
5372

5473
### **Java**
5574

5675
```java
76+
class Solution {
77+
public String fractionAddition(String expression) {
78+
int x = 0, y = 6 * 7 * 8 * 9 * 10;
79+
if (Character.isDigit(expression.charAt(0))) {
80+
expression = "+" + expression;
81+
}
82+
int i = 0, n = expression.length();
83+
while (i < n) {
84+
int sign = expression.charAt(i) == '-' ? -1 : 1;
85+
++i;
86+
int j = i;
87+
while (j < n && expression.charAt(j) != '+' && expression.charAt(j) != '-') {
88+
++j;
89+
}
90+
String s = expression.substring(i, j);
91+
String[] t = s.split("/");
92+
int a = Integer.parseInt(t[0]), b = Integer.parseInt(t[1]);
93+
x += sign * a * y / b;
94+
i = j;
95+
}
96+
int z = gcd(Math.abs(x), y);
97+
x /= z;
98+
y /= z;
99+
return x + "/" + y;
100+
}
101+
102+
private int gcd(int a, int b) {
103+
return b == 0 ? a : gcd(b, a % b);
104+
}
105+
}
106+
```
57107

108+
### **Go**
109+
110+
```go
111+
func fractionAddition(expression string) string {
112+
x, y := 0, 6*7*8*9*10
113+
if unicode.IsDigit(rune(expression[0])) {
114+
expression = "+" + expression
115+
}
116+
i, n := 0, len(expression)
117+
for i < n {
118+
sign := 1
119+
if expression[i] == '-' {
120+
sign = -1
121+
}
122+
i++
123+
j := i
124+
for j < n && expression[j] != '+' && expression[j] != '-' {
125+
j++
126+
}
127+
s := expression[i:j]
128+
t := strings.Split(s, "/")
129+
a, _ := strconv.Atoi(t[0])
130+
b, _ := strconv.Atoi(t[1])
131+
x += sign * a * y / b
132+
i = j
133+
}
134+
z := gcd(abs(x), y)
135+
x /= z
136+
y /= z
137+
return fmt.Sprintf("%d/%d", x, y)
138+
}
139+
140+
func abs(x int) int {
141+
if x < 0 {
142+
return -x
143+
}
144+
return x
145+
}
146+
147+
func gcd(a, b int) int {
148+
if b == 0 {
149+
return a
150+
}
151+
return gcd(b, a%b)
152+
}
58153
```
59154

60155
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
func fractionAddition(expression string) string {
2+
x, y := 0, 6*7*8*9*10
3+
if unicode.IsDigit(rune(expression[0])) {
4+
expression = "+" + expression
5+
}
6+
i, n := 0, len(expression)
7+
for i < n {
8+
sign := 1
9+
if expression[i] == '-' {
10+
sign = -1
11+
}
12+
i++
13+
j := i
14+
for j < n && expression[j] != '+' && expression[j] != '-' {
15+
j++
16+
}
17+
s := expression[i:j]
18+
t := strings.Split(s, "/")
19+
a, _ := strconv.Atoi(t[0])
20+
b, _ := strconv.Atoi(t[1])
21+
x += sign * a * y / b
22+
i = j
23+
}
24+
z := gcd(abs(x), y)
25+
x /= z
26+
y /= z
27+
return fmt.Sprintf("%d/%d", x, y)
28+
}
29+
30+
func abs(x int) int {
31+
if x < 0 {
32+
return -x
33+
}
34+
return x
35+
}
36+
37+
func gcd(a, b int) int {
38+
if b == 0 {
39+
return a
40+
}
41+
return gcd(b, a%b)
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public String fractionAddition(String expression) {
3+
int x = 0, y = 6 * 7 * 8 * 9 * 10;
4+
if (Character.isDigit(expression.charAt(0))) {
5+
expression = "+" + expression;
6+
}
7+
int i = 0, n = expression.length();
8+
while (i < n) {
9+
int sign = expression.charAt(i) == '-' ? -1 : 1;
10+
++i;
11+
int j = i;
12+
while (j < n && expression.charAt(j) != '+' && expression.charAt(j) != '-') {
13+
++j;
14+
}
15+
String s = expression.substring(i, j);
16+
String[] t = s.split("/");
17+
int a = Integer.parseInt(t[0]), b = Integer.parseInt(t[1]);
18+
x += sign * a * y / b;
19+
i = j;
20+
}
21+
int z = gcd(Math.abs(x), y);
22+
x /= z;
23+
y /= z;
24+
return x + "/" + y;
25+
}
26+
27+
private int gcd(int a, int b) {
28+
return b == 0 ? a : gcd(b, a % b);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def fractionAddition(self, expression: str) -> str:
3+
x, y = 0, 6 * 7 * 8 * 9 * 10
4+
if expression[0].isdigit():
5+
expression = '+' + expression
6+
i, n = 0, len(expression)
7+
while i < n:
8+
sign = -1 if expression[i] == '-' else 1
9+
i += 1
10+
j = i
11+
while j < n and expression[j] not in '+-':
12+
j += 1
13+
s = expression[i: j]
14+
a, b = s.split('/')
15+
x += sign * int(a) * y // int(b)
16+
i = j
17+
z = gcd(x, y)
18+
x //= z
19+
y //= z
20+
return f'{x}/{y}'

0 commit comments

Comments
 (0)