Total Accepted: 3726 Total Submissions: 24053 My Submissions Question Solution
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval built-in library function.
Total Accepted: 1046 Total Submissions: 5287 My Submissions Question Solution
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
Note: Do not use the eval built-in library function.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
【解题思路】
1带括号,不带乘除。2不带括号,带乘除
下一个升级版有可能是带括号又带乘除。
1、每次计算都需要判断当前符号是不是括号,如果是括号就先算括号内的值。在discuss中看到一个很漂亮的代码。参考
Iterative Java solution with stack
2、遇乘除计算。然后按照顺序计算一次加减的值。
1、Java AC
public class Solution {
public int calculate(String s) {
Stack<Integer> stack = new Stack<Integer>();
int len = s.length();
int result = 0;
int sign = 1;
int num = 0;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
num = 10 * num + c - '0';
} else if (c == '+') {
result += sign * num;
sign = 1;
num = 0;
} else if (c == '-') {
result += sign * num;
sign = -1;
num = 0;
} else if (c == '(') {
stack.push(result);
stack.push(sign);
result = 0;
sign = 1;
} else if (c == ')') {
result += sign * num;
num = 0;
result *= stack.pop();
result += stack.pop();
}
}
if (num != 0) {
result += sign * num;
}
return result;
}
}
2、Java AC
public class Solution {
public int calculate(String s) {
if (s == null || "".equals(s.trim())) {
return 0;
}
s = s.replaceAll("\\s", "");
List<Long> list1 = new ArrayList<Long>();
List<Character> list2 = new ArrayList<Character>();
int len = s.length();
int i = 0;
while (i < len) {
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
long array[] = getNum(s, i);
list1.add(array[0]);
i = (int) array[1];
} else if (c == '+' || c == '-') {
list2.add(c);
} else if (c == '*' || c == '/') {
int size = list1.size();
long num1 = list1.get(size - 1);
long array[] = getNum(s, i + 1);
long num2 = array[0];
i = (int) array[1];
long num = c == '*' ? num1 * num2 : num1 / num2;
list1.set(size - 1, num);
}
i++;
}
int size1 = list1.size();
int size2 = list2.size();
long preNum = list1.get(0);
for (i = 0; i < size2; i++) {
if (list2.get(i) == '+') {
preNum += list1.get(i + 1);
} else {
preNum -= list1.get(i + 1);
}
}
return (int) preNum;
}
private long[] getNum(String s, int curPos) {
StringBuffer sb = new StringBuffer();
int len = s.length();
while (curPos < len && s.charAt(curPos) >= '0'
&& s.charAt(curPos) <= '9') {
sb.append(s.charAt(curPos));
curPos++;
}
String newStr = sb.toString();
if (newStr.length() > 0) {
return new long[]{Long.parseLong(newStr), curPos - 1};
}
return new long[]{0, curPos - 1};
}
}