• LeetCode-772. Basic Calculator III


    772. Basic Calculator IIILevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/basic-calculator-iii/

    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 .

    The expression string contains only non-negative integers, +, -, *, / operators , open ( and closing parentheses ) and empty spaces . The integer division should truncate toward zero.

    You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647].

    Some examples:

    1. "1 + 1" = 2

    2. " 6-4 / 2 " = 4

    3. "2*(5+5*2)/3+(6/2+8)" = 21

    4. "(2+6* 3+5- (3*14/7+2)*5)+3"=-12

    【C++】

    1. class Solution {
    2. public:
    3. int calculate(string s) {
    4. if (s == nullptr || s.length() == 0) {return 0;}
    5. char sign = '+';
    6. int num = 0, res = 0, len = s.length();
    7. stack<int> stack;
    8. for (int i = 0; i < len; i++) {
    9. char c = s[i];
    10. if (isdigit(c)) {num = num * 10 + c - '0';}
    11. if (!isdigit(c) && c != ' ' || i == len - 1) {
    12. if (c == '(') {
    13. int j = i, int count = 0;
    14. for (; i < len; i++) {
    15. if (s[i] == '(') {count++;}
    16. if (s[i] == ')') {count--;}
    17. if (count == 0) {break;}
    18. }
    19. num = calculate(s.substr(j + 1, i - j - 1));
    20. }
    21. switch (sign) {
    22. case '+': stack.push(num);
    23. case '-': stack.push(-1 * num);
    24. case '*': int a = stack.top(); stack.pop(); stack.push(a * cal);
    25. case '/': int a = stack.top(); stack.pop(); stack.push(a / cal);;
    26. }
    27. num = 0;
    28. sign = c; //before next number
    29. }
    30. }
    31. while (!ns.empty()) {
    32. res += stack.top();
    33. stack.pop();
    34. }
    35. return res;
    36. }
    37. }

    【Java】

    1. import java.util.*
    2. class Solution {
    3. public int calculate(String s) {
    4. if(s == null || s.length() == 0) {return 0;}
    5. char sign = '+';
    6. int num = 0;
    7. int res = 0;
    8. Stack stack = new Stack<>();
    9. int len = s.length();
    10. for (int i = 0; i < len; i++) {
    11. char c = s.charAt(i);
    12. if (Character.isDigit(c)) {num = num * 10 + c - '0';}
    13. if (!Character.isDigit(c) && c != ' ' || i == len - 1) {
    14. if (c == '(') {
    15. int j = i, int count = 0;
    16. for (; i < len; i++) {
    17. if (s.charAt(i) == '(') {count++;}
    18. if (s.charAt(i) == ')') {count--;}
    19. if (count == 0) {break;}
    20. }
    21. num = calculate(s.substring(j + 1, i));
    22. }
    23. switch (sign) {
    24. case '+': stack.push(num);
    25. case '-': stack.push(-num);
    26. case '*': stack.push(stack.pop() * num);
    27. case '/': stack.push(stack.pop() / num);
    28. }
    29. num = 0;
    30. sign = c; //before next number
    31. }
    32. }
    33. for (int val: stack) {res += val;}
    34. return res;
    35. }
    36. }

    another

    1. import java.util.ArrayDeque;
    2. import java.util.Deque;
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. public class Solution {
    6. /**
    7. * @param s: the given expression
    8. * @return: the result of expression
    9. */
    10. public int calculate(String s) {
    11. // Write your code here
    12. Map prio = new HashMap<>();
    13. prio.put('(', 1);
    14. prio.put('+', 2);
    15. prio.put('-', 2);
    16. prio.put('*', 3);
    17. prio.put('/', 3);
    18. Deque ops = new ArrayDeque<>();
    19. Deque stack = new ArrayDeque<>();
    20. for (int i = 0; i < s.length(); i++) {
    21. char ch = s.charAt(i);
    22. if (ch == ' ') {continue;}
    23. if (ch == '(') {
    24. ops.push('(');
    25. } else if (ch == ')') {
    26. while (ops.peek() != '(') {calc(stack, ops);}
    27. ops.pop();
    28. } else if (Character.isDigit(ch)) {
    29. int j = i;
    30. while (j < s.length() && Character.isDigit(s.charAt(j))) {
    31. j++;
    32. }
    33. long num = Long.parseLong(s.substring(i, j));
    34. stack.push(num);
    35. i = j - 1;
    36. } else {
    37. while (!ops.isEmpty() && prio.get(ops.peek()) >= prio.get(ch)) {
    38. calc(stack, ops);
    39. }
    40. ops.push(ch);
    41. }
    42. }
    43. while (stack.size() > 1) {calc(stack, ops);}
    44. long res = stack.peek();
    45. return (int) res;
    46. }
    47. private void calc(Deque stack, Deque ops) {
    48. long n2 = stack.pop(), n1 = stack.pop();
    49. switch (ops.pop()) {
    50. case '+': stack.push(n1 + n2); break;
    51. case '-': stack.push(n1 - n2); break;
    52. case '*': stack.push(n1 * n2); break;
    53. case '/': stack.push(n1 / n2); break;
    54. }
    55. }
    56. }

  • 相关阅读:
    Type-c接口及其协议介绍
    哈希表、无序集合、映射的原理与实现
    谁在遥遥领先
    JavaSE之泛型和通配符
    java数组中删除元素或一个数组元素
    单商户商城系统功能拆解17—供应商分类
    成考报名时间
    SpringBoot统一封装controller层返回的结果
    在windows 10 里安装并设置了gvim 9.0
    (二)springboot整合redis,基于注解快速实现缓存功能
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126114213