Skip to content

Commit 5c7972b

Browse files
add 592
1 parent 618421e commit 5c7972b

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2323
|594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/)|[Solution](../master/src/main/java/com/stevesun/solutions/_594.java) | O(n) |O(n) | Easy | Array, HashMap
2424
|593|[Valid Square](https://leetcode.com/problems/valid-square/)|[Solution](../master/src/main/java/com/stevesun/solutions/_593.java) | O(1) |O(1) | Medium | Math
25+
|592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/)|[Solution](../master/src/main/java/com/stevesun/solutions/_592.java) | O(nlogx) |O(n) | Medium | Math
2526
|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)|[Solution](../master/src/main/java/com/stevesun/solutions/_583.java) | O(m*n) |O(m*n) could be optimized to O(n) | Medium | DP
2627
|582|[Kill Process](https://leetcode.com/problems/kill-process/)|[Solution](../master/src/main/java/com/stevesun/solutions/_582.java) | O(n) |O(h) | Medium | Stack
2728
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Solution](../master/src/main/java/com/stevesun/solutions/_581.java) | O(n) |O(1) | Easy | Array, Sort
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 592. Fraction Addition and Subtraction
8+
*
9+
* Given a string representing an expression of fraction addition and subtraction,
10+
* you need to return the calculation result in string format.
11+
* The final result should be irreducible fraction.
12+
* If your final result is an integer,
13+
* say 2, you need to change it to the format of fraction that has denominator 1.
14+
* So in this case, 2 should be converted to 2/1.
15+
16+
Example 1:
17+
Input:"-1/2+1/2"
18+
Output: "0/1"
19+
20+
Example 2:
21+
Input:"-1/2+1/2+1/3"
22+
Output: "1/3"
23+
24+
Example 3:
25+
Input:"1/3-1/2"
26+
Output: "-1/6"
27+
28+
Example 4:
29+
Input:"5/3+1/3"
30+
Output: "2/1"
31+
32+
Note:
33+
The input string only contains '0' to '9', '/', '+' and '-'. So does the output.
34+
Each fraction (input and output) has format ±numerator/denominator.
35+
If the first input fraction or the output is positive, then '+' will be omitted.
36+
The input only contains valid irreducible fractions,
37+
where the numerator and denominator of each fraction will always be in the range [1,10].
38+
If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
39+
The number of given fractions will be in the range [1,10].
40+
The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
41+
*/
42+
public class _592 {
43+
44+
/**Credit: https://discuss.leetcode.com/topic/89993/java-solution-fraction-addition-and-gcd*/
45+
public String fractionAddition(String expression) {
46+
List<String> nums = new ArrayList<>();
47+
int i = 0;
48+
int j = 0;
49+
while (j <= expression.length()) {
50+
if (j == expression.length() || j != i && (expression.charAt(j) == '-' || expression.charAt(j) == '+')) {
51+
if (expression.charAt(i) == '+') {
52+
nums.add(expression.substring(i+1, j));
53+
} else {
54+
nums.add(expression.substring(i, j));
55+
}
56+
i = j;
57+
}
58+
j++;
59+
}
60+
61+
String result = "0/1";
62+
for (String frac : nums) {
63+
result = add(result, frac);
64+
}
65+
return result;
66+
}
67+
68+
private String add(String result, String frac) {
69+
String[] frac1 = frac.split("/");
70+
String[] frac2 = result.split("/");
71+
int n1 = Integer.parseInt(frac1[0]);
72+
int d1 = Integer.parseInt(frac1[1]);
73+
int n2 = Integer.parseInt(frac2[0]);
74+
int d2 = Integer.parseInt(frac2[1]);
75+
int numerator = n1*d2 + n2*d1;
76+
int denominator = d1*d2;
77+
if (numerator == 0) return "0/1";
78+
79+
boolean negative = numerator*denominator < 0;
80+
numerator = Math.abs(numerator);
81+
denominator = Math.abs(denominator);
82+
int gcd = getGCD(numerator, denominator);
83+
84+
return (negative ? "-" : "") + (numerator/gcd) + "/" + (denominator/gcd);
85+
}
86+
87+
private int getGCD(int a, int b) {
88+
if (a == 0 || b == 0) return a + b;
89+
return getGCD(b, a%b);
90+
}
91+
92+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._592;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 5/23/17.
11+
*/
12+
public class _592Test {
13+
private static _592 test;
14+
private static String expression;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
test = new _592();
19+
}
20+
21+
@Test
22+
public void test1(){
23+
expression = "-1/2+1/2+1/3";
24+
assertEquals("1/3", test.fractionAddition(expression));
25+
}
26+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy