Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created February 16, 2020 02:18
Show Gist options
  • Select an option

  • Save wushbin/e4133240205106c7206faecbf6579194 to your computer and use it in GitHub Desktop.

Select an option

Save wushbin/e4133240205106c7206faecbf6579194 to your computer and use it in GitHub Desktop.
/**
Solution One: Recursion Solution
**/
class Solution {
public int calculate(String s) {
return cal(s, new int[]{0});
}
private int cal(String s, int[] p) {
int res = 0;
int num = 0;
int sign = 1;
while(p[0] < s.length()) {
if (Character.isDigit(s.charAt(p[0]))) {
num = num * 10 + s.charAt(p[0]) - '0';
p[0] ++;
} else if (s.charAt(p[0]) == '+') {
res += (sign * num);
num = 0;
sign = 1;
p[0] ++;
} else if (s.charAt(p[0]) == '-') {
res += (sign * num);
num = 0;
sign = -1;
p[0] ++;
} else if (s.charAt(p[0]) == '(') {
p[0] ++; // skip (
int val = cal(s, p);
res += (sign * val);
} else if (s.charAt(p[0]) == ')') {
p[0] ++;
res += (sign * num);
return res;
} else {
p[0] ++;
}
}
res += (sign * num);
return res;
}
}
/**
Solution Two
Non Recursion
**/
class Solution {
public int calculate(String s) {
int res = 0;
int num = 0;
int sign = 1;
int p = 0;
Deque<Integer> stack = new ArrayDeque<>();
while(p < s.length()) {
if (Character.isDigit(s.charAt(p))) {
num = num * 10 + s.charAt(p) - '0';
p++;
} else if (s.charAt(p) == '+') {
res += (sign * num);
num = 0;
sign = 1;
p++;
} else if (s.charAt(p) == '-') {
res += (sign * num);
num = 0;
sign = -1;
p++;
} else if (s.charAt(p) == '(') {
p++;
stack.addLast(res);
stack.addLast(sign);
res = 0;
num = 0;
sign = 1;
} else if (s.charAt(p) == ')') {
p++;
res += (sign * num);
int preSign = stack.pollLast();
int preRes = stack.pollLast();
num = 0;
sign = 1;
res = preSign * res + preRes;
} else {
p++;
}
}
res += (sign * num);
return res;
}
}
/**
Solution Three
May support multiply and divie, really slow
**/
class Solution {
public int calculate(String s) {
Deque<Integer> stack = new ArrayDeque<>();
Deque<Character> ops = new ArrayDeque<>();
int idx = 0;
int len = s.length();
boolean seeNum = false;
int num = 0;
while(idx < len) {
char c = s.charAt(idx);
if (Character.isDigit(c)) {
seeNum = true;
num = num * 10 + c - '0';
} else if ( c == '+' || c == '-' || c == ')') {
if (stack.isEmpty()) {
stack.addLast(num);
} else if (!ops.isEmpty() && ops.peekLast() == '(') {
if (seeNum) {
stack.addLast(num);
}
} else if (!ops.isEmpty()) {
char op = ops.pollLast();
int preNum = stack.pollLast();
stack.addLast(cal(preNum, num, op));
}
if (c == ')') {
ops.pollLast(); // remove (
if (!ops.isEmpty() && ops.peekLast() != '(') {
char op = ops.pollLast();
int num2 = stack.pollLast();
int num1 = stack.pollLast();
stack.addLast(cal(num1, num2, op));
}
} else {
ops.addLast(c);
}
num = 0;
seeNum = false;
} else if (c == '(') {
ops.addLast(c);
}
idx += 1;
}
if (!ops.isEmpty() && ops.peekLast() != '(') {
char op = ops.pollLast();
int preNum = stack.pollLast();
stack.addLast(cal(preNum, num, op));
}
return stack.isEmpty() ? num : stack.pollLast();
}
private int cal(int num1, int num2, char op) {
System.out.println(num1 + " " + op + " " + num2);
if (op == '+') {
return num1 + num2;
} else if (op == '-') {
return num1 - num2;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment