Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita3c3e68

Browse files
refactor 592
1 parent0c8eefc commita3c3e68

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

‎src/main/java/com/fishercoder/solutions/_592.java

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,56 +41,61 @@ Each fraction (input and output) has format ±numerator/denominator.
4141
*/
4242
publicclass_592 {
4343

44-
/**Credit: https://discuss.leetcode.com/topic/89993/java-solution-fraction-addition-and-gcd*/
45-
publicStringfractionAddition(Stringexpression) {
46-
List<String>nums =newArrayList<>();
47-
inti =0;
48-
intj =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+
publicstaticclassSolution1 {
45+
46+
/**
47+
* Credit: https://discuss.leetcode.com/topic/89993/java-solution-fraction-addition-and-gcd
48+
*/
49+
publicStringfractionAddition(Stringexpression) {
50+
List<String>nums =newArrayList<>();
51+
inti =0;
52+
intj =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;
5561
}
56-
i =j;
62+
j++;
5763
}
58-
j++;
59-
}
6064

61-
Stringresult ="0/1";
62-
for (Stringfrac :nums) {
63-
result =add(result,frac);
65+
Stringresult ="0/1";
66+
for (Stringfrac :nums) {
67+
result =add(result,frac);
68+
}
69+
returnresult;
6470
}
65-
returnresult;
66-
}
6771

68-
privateStringadd(Stringresult,Stringfrac) {
69-
String[]frac1 =frac.split("/");
70-
String[]frac2 =result.split("/");
71-
intn1 =Integer.parseInt(frac1[0]);
72-
intd1 =Integer.parseInt(frac1[1]);
73-
intn2 =Integer.parseInt(frac2[0]);
74-
intd2 =Integer.parseInt(frac2[1]);
75-
intnumerator =n1 *d2 +n2 *d1;
76-
intdenominator =d1 *d2;
77-
if (numerator ==0) {
78-
return"0/1";
79-
}
72+
privateStringadd(Stringresult,Stringfrac) {
73+
String[]frac1 =frac.split("/");
74+
String[]frac2 =result.split("/");
75+
intn1 =Integer.parseInt(frac1[0]);
76+
intd1 =Integer.parseInt(frac1[1]);
77+
intn2 =Integer.parseInt(frac2[0]);
78+
intd2 =Integer.parseInt(frac2[1]);
79+
intnumerator =n1 *d2 +n2 *d1;
80+
intdenominator =d1 *d2;
81+
if (numerator ==0) {
82+
return"0/1";
83+
}
8084

81-
booleannegative =numerator *denominator <0;
82-
numerator =Math.abs(numerator);
83-
denominator =Math.abs(denominator);
84-
intgcd =getGCD(numerator,denominator);
85+
booleannegative =numerator *denominator <0;
86+
numerator =Math.abs(numerator);
87+
denominator =Math.abs(denominator);
88+
intgcd =getGCD(numerator,denominator);
8589

86-
return (negative ?"-" :"") + (numerator /gcd) +"/" + (denominator /gcd);
87-
}
90+
return (negative ?"-" :"") + (numerator /gcd) +"/" + (denominator /gcd);
91+
}
8892

89-
privateintgetGCD(inta,intb) {
90-
if (a ==0 ||b ==0) {
91-
returna +b;
93+
privateintgetGCD(inta,intb) {
94+
if (a ==0 ||b ==0) {
95+
returna +b;
96+
}
97+
returngetGCD(b,a %b);
9298
}
93-
returngetGCD(b,a %b);
9499
}
95100

96101
}

‎src/test/java/com/fishercoder/_592Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
* Created by fishercoder on 5/23/17.
1111
*/
1212
publicclass_592Test {
13-
privatestatic_592test;
13+
privatestatic_592.Solution1test;
1414
privatestaticStringexpression;
1515

1616
@BeforeClass
1717
publicstaticvoidsetup() {
18-
test =new_592();
18+
test =new_592.Solution1();
1919
}
2020

2121
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp