zoukankan      html  css  js  c++  java
  • [leetcode]Evaluate Reverse Polish Notation

    逆波兰表达式,情况不是很复杂,用一个栈就解决了。

    #include <stack>
    #include <string>
    using namespace std;
    
    class Solution {
    public:
        int evalRPN(vector<string> &tokens) {
            for (int i = 0; i < tokens.size(); i++) {
                string &token = tokens[i];
                if (token == "+") {
                    int a = num_stack.top();
                    num_stack.pop();
                    int b = num_stack.top();
                    num_stack.pop();
                    int r = a + b;
                    num_stack.push(r);
                } else if (token == "-") {
                    int a = num_stack.top();
                    num_stack.pop();
                    int b = num_stack.top();
                    num_stack.pop();
                    int r = b - a;
                    num_stack.push(r);
                } else if (token == "*") {
                    int a = num_stack.top();
                    num_stack.pop();
                    int b = num_stack.top();
                    num_stack.pop();
                    int r = a * b;
                    num_stack.push(r);
                } else if (token == "/") {
                    int a = num_stack.top();
                    num_stack.pop();
                    int b = num_stack.top();
                    num_stack.pop();
                    int r = b / a;
                    num_stack.push(r);
                } else { // number
                    bool neg = (token[0] == '-');
                    int pos = 0;
                    if (neg) {
                        pos = 1;
                    }
                    int r = 0;
                    for (int k = pos; k < token.length(); k++) {
                        r = r * 10 + (token[k] - '0');
                    }
                    if (neg) {
                        r = -r;
                    }
                    num_stack.push(r);
                }
            }
            int result = num_stack.top();
            num_stack.pop();
            return result;
        }
    
    private:
        stack<int> num_stack;
    };
    

    中间那些部分可以简化出来。

    int o1, o2;  
    o2 = numeric.top();  
    numeric.pop();  
    o1 = numeric.top();  
    numeric.pop();  
    
    switch(t[0])
    {
        case '+':
            numeric.push(o1 + o2);
            break;
        case '-':
            numeric.push(o1 - o2);
            break;
        case '*':
            numeric.push(o1 * o2);
            break;
        case '/':
            numeric.push(o1 / o2);
            break;
    }
    

    第二刷,Annie的解法比较简洁。且其中stoi可以直接将字符串转成整数。

    python3,要注意//代表floor division

    class Solution:
        def evalRPN(self, tokens: List[str]) -> int:
            stack = []
            for token in tokens:
                if token in ['+', '-', '*', '/']:
                    num2 = stack.pop()
                    num1 = stack.pop()
                    if token == '+':
                        stack.append(num1 + num2)
                    elif token == '-':
                        stack.append(num1 - num2)
                    elif token == '*':
                        stack.append(num1 * num2)
                    elif token == '/':
                        stack.append(int(num1 / num2))
                else:
                    stack.append(int(token))
            
            return stack.pop()
    

      

  • 相关阅读:
    两个链表的第一个公共结点
    数组中的逆序对
    第一个只出现一次的字符(字符流中第一个只出现一次的字符)
    丑数
    最长不含有重复字符的子字符串
    礼物的最大价值
    把数字翻译成字符串
    把数组排成最小的数
    [CSP-S模拟测试]:赤(red)(WQS二分+DP)
    [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)
  • 原文地址:https://www.cnblogs.com/lautsie/p/3512287.html
Copyright © 2011-2022 走看看