-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path640.java
More file actions
157 lines (143 loc) · 4.73 KB
/
640.java
File metadata and controls
157 lines (143 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public String solveEquation(String equation) {
int indexEqual = equation.indexOf("=");
String leftString=equation.substring(0,indexEqual);
String rightString=equation.substring(indexEqual+1);
int[] leftParts=parse(leftString);
int[] rightParts=parse(rightString);
if(leftParts[0]==rightParts[0]){
if(leftParts[1]==rightParts[1]){
return "Infinite solutions";
}
else{
return "No solution";
}
}
else{
//System.out.println(rightParts[0]+","+rightParts[1]);
int x = leftParts[0]-rightParts[0];
int y = leftParts[1]-rightParts[1];
int result= -y/x;
return "x="+result;
}
}
private int[] parseParts(String str,int start,int end){
int[] result = new int[2];
String partStr = str.substring(start,end);
//System.out.println(partStr);
if(partStr.equals("")) return result;
int index=1;
int length=partStr.length();
if(partStr.indexOf("x")!=-1){
index=0;
length--;
}
int num=0;
for(int i=0;i<length;i++){
int current=partStr.charAt(i);
num=num*10+(current-'0');
}
if(index==0&&length==0){
num=1;
}
result[index]=num;
return result;
}
private int[] parse(String str){
int start=0;
int[] result = new int[2];
boolean plusFlag=true;
while(start<str.length()){
int posMinus = str.indexOf("-",start);
if(posMinus==-1) posMinus=str.length();
int posPlus =str.indexOf("+",start);
if(posPlus==-1) posPlus=str.length();
int pos=Math.min(posMinus,posPlus);
int[] parts = parseParts(str,start,pos);
///System.out.println(parts[0]+","+parts[1]);
if(plusFlag){
result[0]+=parts[0];
result[1]+=parts[1];
}
else{
result[0]-=parts[0];
result[1]-=parts[1];
}
if(posMinus!=str.length()&&pos==posMinus){
plusFlag=false;
}
else{
plusFlag=true;
}
start=pos+1;
}
return result;
}
}
__________________________________________________________________________________________________
sample 35384 kb submission
class Solution {
public String solveEquation(String equation) {
String[] equations = equation.split("=");
int countX = 0;
int countD = 0;
int[] left = countXD(equations[0]);
int[] right = countXD(equations[1]);
countX = left[0] - right[0];
countD = right[1] - left[1];
if(countX == 0) {
return countD == 0 ? "Infinite solutions" : "No solution";
}
return "x=" + String.valueOf(countD / countX);
}
private int[] countXD(String eq) {
boolean posi = true;
int countX = 0;
int countD = 0;
int tempSum = 0;
for(int i = 0; i < eq.length(); i++) {
char cur = eq.charAt(i);
if(Character.isDigit(cur)) {
tempSum = tempSum * 10 + (cur - '0');
} else if(cur == 'x') {
if(posi) {
if(i == 0 || !Character.isDigit(eq.charAt(i - 1))) {
countX += 1;
} else {
countX += tempSum;
}
} else {
if(i == 0 || !Character.isDigit(eq.charAt(i - 1))) {
countX -= 1;
} else {
countX -= tempSum;
}
}
tempSum = 0;
} else if(cur == '+' || cur == '-') {
if(posi) {
countD += tempSum;
} else {
countD -= tempSum;
}
tempSum = 0;
posi = cur == '+' ? true : false;
}
}
//post processing
if(tempSum != 0) {
if(posi) {
countD += tempSum;
} else {
countD -= tempSum;
}
}
int[] result = new int[2];
result[0] = countX;
result[1] = countD;
return result;
}
}
__________________________________________________________________________________________________