Implement a basic calculator to evaluate a simple expression string.javascript
The expression string contains only non-negative integers, +
, -
, *
, /
operators and empty spaces ``. The integer division should truncate toward zero.html
Example 1:java
Input: "3+2*2" Output: 7
Example 2:express
Input: " 3/2 " Output: 1
Example 3:ui
Input: " 3+5 / 2 " Output: 5
Note:lua
eval
built-in library function.計算只包含+
, -
, *
, /
和空格的數學表達式的值。spa
方法一:從後向前遍歷字符串,遇空格跳過,遇*
, /
壓入操做符棧中,遇數字壓入操做數棧中,遇'+', '-'須要進行判斷:若是操做符棧棧頂爲*
或/
,則從操做數棧和操做符棧分別出棧,將計算結果壓回操做數棧,重複上述過程直到操做符棧爲空或其棧頂爲+
, -
,再將當前的+
, -
壓入操做符棧中;其他狀況則直接將+
, -
壓入操做符棧中。所有遍歷完後,重複出棧操做數棧和操做符棧計算結果便可。code
方法二:從前向後遍歷,參考自 [LeetCode] 227. Basic Calculator II 基本計算器之二。htm
class Solution { public int calculate(String s) { Deque<Integer> nums = new ArrayDeque<>(); Deque<Character> ops = new ArrayDeque<>(); for (int i = s.length() - 1; i >= 0; i--) { char c = s.charAt(i); if (c == ' ') { continue; } else if (c == '+' || c == '-') { while (!ops.isEmpty() && (ops.peek() == '*' || ops.peek() == '/')) { int a = nums.pop(); int b = nums.pop(); char op = ops.pop(); int cal = op == '*' ? a * b : a / b; nums.push(cal); } ops.push(c); } else if (c == '*' || c == '/') { ops.push(c); } else { int num = c - '0'; int zeros = 10; while (i - 1 >= 0 && s.charAt(i - 1) <= '9' && s.charAt(i - 1) >= '0') { num = (s.charAt(i - 1) - '0') * zeros + num; zeros *= 10; i--; } nums.push(num); } } while (nums.size() != 1) { int a = nums.pop(); int b = nums.pop(); char op = ops.pop(); int cal = (op == '+' ? a + b : op == '-' ? a - b : op == '*' ? a * b : a / b); nums.push(cal); } return nums.pop(); } }
class Solution { public int calculate(String s) { Deque<Integer> stack = new ArrayDeque<>(); int factor = 1; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ' ') { continue; } else if (c == '*' || c == '/') { int a = stack.pop(); int b = 0; while (!(s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9')) { i++; } while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') { b = b * 10 + s.charAt(i + 1) - '0'; i++; } stack.push(c == '*' ? a * b : a / b); } else if (c == '+' || c == '-') { factor = c == '+' ? 1 : -1; } else { int num = c - '0'; while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') { num = num * 10 + s.charAt(i + 1) - '0'; i++; } stack.push(num * factor); } } while (stack.size() != 1) { stack.push(stack.pop() + stack.pop()); } return stack.pop(); } }
/** * @param {string} s * @return {number} */ var calculate = function (s) { let nums = [] let op = 1 let i = 0 let reg = /[0-9]/ while (i < s.length) { if (reg.test(s[i])) { let num = parseInt(s[i]) while (++i < s.length && reg.test(s[i])) { num = num * 10 + parseInt(s[i]) } nums.push(op * num) } else if (s[i] === '+' || s[i] === '-') { op = s[i++] === '+' ? 1 : -1 } else if (s[i] === '*' || s[i] === '/') { let c = s[i] let A = nums.pop() while (s[++i] === ' ') {} let B = parseInt(s[i]) while (++i < s.length && reg.test(s[i])) { B = B * 10 + parseInt(s[i]) } nums.push(c === '*' ? A * B : Math.trunc(A / B)) } else { i++ } } return nums.reduce((acc, cur) => acc + cur) }