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

Commitec0b396

Browse files
refactor 640
1 parent3a2d3bc commitec0b396

File tree

1 file changed

+28
-26
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-26
lines changed

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,37 @@
3434
3535
*/
3636
publicclass_640 {
37-
/**
38-
* Reference: https://discuss.leetcode.com/topic/95203/concise-java-solution/7
39-
*/
40-
publicStringsolveEquation(Stringequation) {
41-
String[]parts =equation.split("=");
42-
int[]left =evaluate(parts[0]);
43-
int[]right =evaluate(parts[1]);
44-
if (left[0] ==right[0] &&left[1] ==right[1]) {
45-
return"Infinite solutions";
46-
}elseif (left[0] ==right[0]) {
47-
return"No solution";
37+
publicstaticclassSolution1 {
38+
/**
39+
* Reference: https://discuss.leetcode.com/topic/95203/concise-java-solution/7
40+
*/
41+
publicStringsolveEquation(Stringequation) {
42+
String[]parts =equation.split("=");
43+
int[]left =evaluate(parts[0]);
44+
int[]right =evaluate(parts[1]);
45+
if (left[0] ==right[0] &&left[1] ==right[1]) {
46+
return"Infinite solutions";
47+
}elseif (left[0] ==right[0]) {
48+
return"No solution";
49+
}
50+
return"x=" + (right[1] -left[1]) / (left[0] -right[0]);
4851
}
49-
return"x=" + (right[1] -left[1]) / (left[0] -right[0]);
50-
}
5152

52-
privateint[]evaluate(Stringpart) {
53-
int[]result =newint[2];//result[0] is the coefficient for x, result[1] is the coefficient for constants
54-
String[]tokens =part.split("(?=[+-])");// ()for match group; ?= for match and include in res; [+-] means + or -;
55-
for (Stringtoken :tokens) {
56-
if (token.equals("+x") ||token.equals("x")) {
57-
result[0]++;
58-
}elseif (token.equals("-x")) {
59-
result[0]--;
60-
}elseif (token.contains("x")) {
61-
result[0] +=Integer.parseInt(token.substring(0,token.length() -1));
62-
}else {
63-
result[1] +=Integer.parseInt(token);
53+
privateint[]evaluate(Stringpart) {
54+
int[]result =newint[2];//result[0] is the coefficient for x, result[1] is the coefficient for constants
55+
String[]tokens =part.split("(?=[+-])");// ()for match group; ?= for match and include in res; [+-] means + or -;
56+
for (Stringtoken :tokens) {
57+
if (token.equals("+x") ||token.equals("x")) {
58+
result[0]++;
59+
}elseif (token.equals("-x")) {
60+
result[0]--;
61+
}elseif (token.contains("x")) {
62+
result[0] +=Integer.parseInt(token.substring(0,token.length() -1));
63+
}else {
64+
result[1] +=Integer.parseInt(token);
65+
}
6466
}
67+
returnresult;
6568
}
66-
returnresult;
6769
}
6870
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp