@@ -41,56 +41,61 @@ Each fraction (input and output) has format ±numerator/denominator.
41
41
*/
42
42
public class _592 {
43
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 ));
44
+ public static class Solution1 {
45
+
46
+ /**
47
+ * Credit: https://discuss.leetcode.com/topic/89993/java-solution-fraction-addition-and-gcd
48
+ */
49
+ public String fractionAddition (String expression ) {
50
+ List <String >nums =new ArrayList <>();
51
+ int i =0 ;
52
+ int j =0 ;
53
+ while (j <=expression .length ()) {
54
+ if (j ==expression .length () ||j !=i && (expression .charAt (j ) =='-' ||expression .charAt (j ) =='+' )) {
55
+ if (expression .charAt (i ) =='+' ) {
56
+ nums .add (expression .substring (i +1 ,j ));
57
+ }else {
58
+ nums .add (expression .substring (i ,j ));
59
+ }
60
+ i =j ;
55
61
}
56
- i = j ;
62
+ j ++ ;
57
63
}
58
- j ++;
59
- }
60
64
61
- String result ="0/1" ;
62
- for (String frac :nums ) {
63
- result =add (result ,frac );
65
+ String result ="0/1" ;
66
+ for (String frac :nums ) {
67
+ result =add (result ,frac );
68
+ }
69
+ return result ;
64
70
}
65
- return result ;
66
- }
67
71
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 ) {
78
- return "0/1" ;
79
- }
72
+ private String add (String result ,String frac ) {
73
+ String []frac1 =frac .split ("/" );
74
+ String []frac2 =result .split ("/" );
75
+ int n1 =Integer .parseInt (frac1 [0 ]);
76
+ int d1 =Integer .parseInt (frac1 [1 ]);
77
+ int n2 =Integer .parseInt (frac2 [0 ]);
78
+ int d2 =Integer .parseInt (frac2 [1 ]);
79
+ int numerator =n1 *d2 +n2 *d1 ;
80
+ int denominator =d1 *d2 ;
81
+ if (numerator ==0 ) {
82
+ return "0/1" ;
83
+ }
80
84
81
- boolean negative =numerator *denominator <0 ;
82
- numerator =Math .abs (numerator );
83
- denominator =Math .abs (denominator );
84
- int gcd =getGCD (numerator ,denominator );
85
+ boolean negative =numerator *denominator <0 ;
86
+ numerator =Math .abs (numerator );
87
+ denominator =Math .abs (denominator );
88
+ int gcd =getGCD (numerator ,denominator );
85
89
86
- return (negative ?"-" :"" ) + (numerator /gcd ) +"/" + (denominator /gcd );
87
- }
90
+ return (negative ?"-" :"" ) + (numerator /gcd ) +"/" + (denominator /gcd );
91
+ }
88
92
89
- private int getGCD (int a ,int b ) {
90
- if (a ==0 ||b ==0 ) {
91
- return a +b ;
93
+ private int getGCD (int a ,int b ) {
94
+ if (a ==0 ||b ==0 ) {
95
+ return a +b ;
96
+ }
97
+ return getGCD (b ,a %b );
92
98
}
93
- return getGCD (b ,a %b );
94
99
}
95
100
96
101
}