1. var calculate = function(s) {
    2. s = s.trim();
    3. const stack = new Array();
    4. let preSign = '+';
    5. let num = 0;
    6. const n = s.length;
    7. for (let i = 0; i < n; ++i) {
    8. if (!isNaN(Number(s[i])) && s[i] !== ' ') {
    9. num = num * 10 + s[i].charCodeAt() - '0'.charCodeAt();
    10. }
    11. if (isNaN(Number(s[i])) || i === n - 1) {
    12. switch (preSign) {
    13. case '+':
    14. stack.push(num);
    15. break;
    16. case '-':
    17. stack.push(-num);
    18. break;
    19. case '*':
    20. stack.push(stack.pop() * num);
    21. break;
    22. default:
    23. stack.push(stack.pop() / num | 0);
    24. }
    25. preSign = s[i];
    26. num = 0;
    27. }
    28. }
    29. let ans = 0;
    30. while (stack.length) {
    31. ans += stack.pop();
    32. }
    33. return ans;
    34. };
    35. 作者:LeetCode-Solution
    36. 链接:https://leetcode-cn.com/problems/basic-calculator-ii/solution/ji-ben-ji-suan-qi-ii-by-leetcode-solutio-cm28/
    37. 来源:力扣(LeetCode
    38. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。