leetcode-栈练习-evaluate-reverse-polish-notation

evaluate-reverse-polish-notation

https://www.nowcoder.com/practice/22f9d7dd89374b6c8289e44237c70447?tpId=46&tqId=29031&tPage=1&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking

计算逆波兰式(后缀表达式)的值
运算符仅包含"+","-","*"和"/",被操作数可能是整数或其他表达式
例如:

就是简单的栈应用

import java.util.Stack;
public class Solution {
    public int evalRPN(String[] tokens) {

        Stack<Integer> stack = new Stack<>();
        for(int i=0; i < tokens.length; i++){

            if(tokens[i].equals("+")){
                int op1 = stack.pop();
                int op2 = stack.pop();
                stack.push(op1+op2);
                continue;
            }
            i

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

小白刷Leetcode 文章被收录于专栏

那些必刷的leetcode

全部评论
嫌代码多的话可以先加在第一个if前面加一重if判断是否为符号,把出栈放原先的第一个if外面,然后除原先第一个if以外的if都改成else if,最后那个入栈再加上else,这样if里面的continue也可以去掉了。
点赞 回复 分享
发布于 2020-04-12 16:52

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务