逆波兰表达式求值

[题](逆波兰表达式求值_牛客题霸_牛客网 (nowcoder.com))

  1. 代码

    自己的版本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    class Solution {
    public:
    /**
    * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    *
    *
    * @param tokens string字符串vector
    * @return int整型
    */
    int evalRPN(vector<string>& tokens) {
    // write code here
    stack<int> stack;
    int num;
    int sum=0;//return
    for(const auto& item:tokens){
    try{
    num=std::stoi(item.data());
    stack.push(num);
    }catch (...){
    if(item=="+"){
    num=stack.top();
    stack.pop();
    sum=stack.top()+num;
    stack.pop();
    stack.push(sum);
    } else if(item=="-"){
    num=stack.top();
    stack.pop();
    sum=stack.top()-num;
    stack.pop();
    stack.push(sum);
    }else if(item=="*"){
    num=stack.top();
    stack.pop();
    sum=num*stack.top();
    stack.pop();
    stack.push(sum);
    } else if(item=="/"){
    num=stack.top();
    stack.pop();
    sum=stack.top()/num;
    stack.pop();
    stack.push(sum);
    }else {
    cout<<"input error"<<endl;
    }
    }
    }
    return stack.top();
    }
    };

    /**
    * 存在的问题是,选择太太多了,代码冗长
    * try,catch的性能消耗过大
    */

    答案

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    class Solution {
    public:
    /**
    * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    *
    *
    * @param tokens string字符串vector
    * @return int整型
    */
    int evalRPN(vector<string>& tokens) {
    // write code here
    stack<int> stack;
    for(const auto& item: tokens){
    if(item=="+"||item=="-"||item=="*"||item=="/"){
    int num=stack.top();
    stack.pop();
    int num2=stack.top();
    stack.pop();
    if(item=="+"){
    stack.push(num2+num);
    }else if(item=="-"){
    stack.push(num2-num);
    }else if(item=="*"){
    stack.push(num*num2);
    }else if(item=="/"){
    stack.push(num2/num);
    }
    }else{
    stack.push(std::stoi(item));
    }
    }
    return stack.top();
    }
    };
  2. 提示

  3. 理解

    • 通过压栈存放临时数据
  4. 问题

    • 为什么try``catch语句,性能消耗大?

      使用 trycatch 可以在程序运行时捕获并处理异常。然而,由于异常处理需要额外的开销,如果在代码中频繁地使用 trycatch,会对程序的性能产生影响。

      具体来说,当程序出现异常时,C++ 运行时系统需要在堆栈上创建一个异常对象,并执行一系列系统调用来查找匹配的 catch 块。这个过程需要进行堆栈展开(stack unwinding),将程序从异常抛出点回退到能够处理异常的 catch 块处。由于堆栈展开需要降低程序的执行速度,并且需要额外的内存和调用开销,因此异常处理可能会对程序的性能产生负面影响。

      总的来说,在代码中使用 trycatch 不会直接影响程序的性能,但是在发生异常时需要进行额外的处理,从而可能导致程序变慢。因此,在实际编写代码时,应该避免滥用异常处理,仅在必要时使用 trycatch 来处理异常,以提高程序的性能和效率。


逆波兰表达式求值
https://tsy244.github.io/2023/04/19/算法/newcoder/逆波兰表达式求值/
Author
August Rosenberg
Posted on
April 19, 2023
Licensed under