Leetcode: Evaluate Reverse Polish Notation

迷南。 2022-08-05 14:28 115阅读 0赞

题目:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Some examples:

[“2”, “1”, “+”, “3”, ““] -> ((2 + 1) 3) -> 9
[“4”, “13”, “5”, “/”, “+”] -> (4 + (13 / 5)) -> 6

分析:
所谓的逆波兰表达式就是后缀表达式,参见我的另一篇博客:计算器:中缀表达式转后缀表达式
这道题的解法同样是利用栈,遇到数字压栈,遇到运算符栈顶两个数字进行运算,结果压栈,最后返回栈顶数字即可。

C++代码:

  1. class Solution
  2. {
  3. public:
  4. int evalRPN(vector<string> &tokens)
  5. {
  6. stack<int> operands;
  7. size_t size = tokens.size();
  8. int operandX, operandY;
  9. int result;
  10. for (size_t i = 0; i < size; i++)
  11. {
  12. if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/")
  13. {
  14. operandY = operands.top();
  15. operands.pop();
  16. operandX = operands.top();
  17. operands.pop();
  18. //C++的switch语句不支持string类型
  19. switch (tokens[i][0])
  20. {
  21. case '+':
  22. result = operandX + operandY;
  23. break;
  24. case '-':
  25. result = operandX - operandY;
  26. break;
  27. case '*':
  28. result = operandX * operandY;
  29. break;
  30. case '/':
  31. result = operandX / operandY;
  32. break;
  33. default:
  34. break;
  35. }
  36. operands.push(result);
  37. }
  38. else
  39. {
  40. operands.push(atoi(tokens[i].c_str()));
  41. }
  42. }
  43. return operands.top();
  44. }
  45. };

C#代码:

  1. public class Solution
  2. {
  3. public int EvalRPN(string[] tokens)
  4. {
  5. Stack<int> operands = new Stack<int>();
  6. int operandX, operandY;
  7. int result = 0;
  8. foreach (string item in tokens)
  9. {
  10. if (item == "+" || item == "-" || item == "*" || item == "/")
  11. {
  12. operandY = operands.Pop();
  13. operandX = operands.Pop();
  14. switch (item)
  15. {
  16. case "+":
  17. result = operandX + operandY;
  18. break;
  19. case "-":
  20. result = operandX - operandY;
  21. break;
  22. case "*":
  23. result = operandX * operandY;
  24. break;
  25. case "/":
  26. result = operandX / operandY;
  27. break;
  28. default:
  29. break;
  30. }
  31. operands.Push(result);
  32. }
  33. else
  34. {
  35. operands.Push(Int32.Parse(item));
  36. }
  37. }
  38. return operands.Peek();
  39. }
  40. }

Python代码:
注意:1. Python中没有switch…case语句;2. Python中的除法和C语言中的除法不一样,所以对负数我们要做特殊处理,得到的结果才能和C系语言的结果一样

  1. class Solution:
  2. # @param tokens, a list of string
  3. # @return an integer
  4. def evalRPN(self, tokens):
  5. operands = []
  6. operators = ['+', '-', '*', '/']
  7. for i in range(len(tokens)):
  8. if tokens[i] in operators:
  9. operandy = operands.pop()
  10. operandx = operands.pop()
  11. if tokens[i] == '+':
  12. result = operandx + operandy
  13. elif tokens[i] == '-':
  14. result = operandx - operandy
  15. elif tokens[i] == '*':
  16. result = operandx * operandy
  17. #because the division operation is differnt from the C
  18. #so here we must hadle the negative number
  19. elif tokens[i] == '/':
  20. if (operandx > 0) ^ (operandy > 0):
  21. result = -1 * (abs(operandx) / abs(operandy))
  22. else:
  23. result = operandx / operandy
  24. operands.append(result)
  25. else:
  26. operands.append(int(tokens[i]))
  27. return operands.pop()

发表评论

表情:
评论列表 (有 0 条评论,115人围观)

还没有评论,来说两句吧...

相关阅读