LeetCode中使用栈的题目的整理(待更)

本文整理了LeetCode中涉及栈的题目,包括棒球比赛(简单)、简化路径(中等难度)和逆波兰表达式求值(中等难度)。提供了各题目的解题思路和代码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

棒球比赛(simple)

https://leetcode-cn.com/circle/article/48kq9d/

class Solution {
    public int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack();

        for(String op : ops) {
            if (op.equals("+")) {
                int top = stack.pop();
                int newtop = top + stack.peek();
                stack.push(top);
                stack.push(newtop);
            } else if (op.equals("C")) {
                stack.pop();
            } else if (op.equals("D")) {
                stack.push(2 * stack.peek());
            } else {
                stack.push(Integer.valueOf(op));
            }
        }

        int ans = 0;
        for(int score : stack) ans += score;
        return ans;
    }
}


作者:LeetCode
链接:https://leetcode-cn.com/problems/baseball-game/solution/bang-qiu-bi-sai-by-leetcode/
来源:力扣(LeetCode)

本方法思路和代码来源:

作者:Jade_Xie
链接:https://leetcode-cn.com/problems/baseball-game/solution/java-1ms-ji-bai-100-shu-zu-de-si-xiang-b-yvo3/
来源:力扣(LeetCode)

class Solution {
    public int calPoints(String[] ops) {
        //数组长度
        int length = ops.length;

        //建立一个数字数组,用来存储每次的操作结果
        int[] num = new int[length];
        //数字数组的当前下标
        int index = 0;

        //遍历符号数组
        for(int i = 0; i < length; i++)
        {
            //当前符号为“C”,需要清除一个数字
            if(ops[i].equals("C"))
            {
                //下标回退一格,且回退后位置上的数字置零
                index --;
                num[index] = 0;
            }
            //当前符号为“D”,需要翻倍
            else if(ops[i].equals("D"))
            {
                //当前数字等于前一个数字的两倍
                num[index] = 2 * num[index - 1];
                index ++;
            }
            //当前符号为“+”,需要相加
            else if(ops[i].equals("+"))
            {
                //当前数字等于前两个数字之和
                num[index] = num[index - 1] + num[index - 2];
                index ++;
            }
            //当前符号为数字,直接填入数字数组
            else
            {
                num[index] = Integer.parseInt(ops[i]);
                index ++;
            }
        }

        //对数字数组进行求和
        int sum = 0;
        for(int j = 0; j < length; j++)
        {
            sum += num[j];
        }

        return sum;

    }
}


作者:Jade_Xie
链接:https://leetcode-cn.com/problems/baseball-game/solution/java-1ms-ji-bai-100-shu-zu-de-si-xiang-b-yvo3/
来源:力扣(LeetCode)

简化路径(medium难度)

https://leetcode-cn.com/problems/simplify-path/

代码及思路来源:

作者:LeetcodeFrom20210207
链接:https://leetcode-cn.com/problems/simplify-path/solution/71-jian-hua-lu-jing-by-leetcodefrom20210-c9uv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

class Solution {
    public String simplifyPath(String path) {
        LinkedList<String> stack = new LinkedList<String>();//用栈来模拟
        String[] strArr = path.split("/");//用/分割,多个/也视为/
        for(String str: strArr){//遍历
            //如果等于空或者等于.,那就没有影响
            if(str.equals("") || str.equals(".")){
                continue;
            }
            //如果等于..,那就要返回上一级目录,因此栈中弹出当前目录
            //此时可能栈是空
            if(str.equals("..")){
                if(!stack.isEmpty()){
                    stack.pop();
                }
                continue;
            }
            //否则,栈中压入当前目录
            stack.push(str);
        }
        StringBuffer sb = new StringBuffer();
        Collections.reverse(stack);
        while(!stack.isEmpty()){
            String tmp = stack.pop();
            sb.append("/").append(tmp);
        }
        if(sb.length() == 0){
            sb.append("/");
        }
        String res = sb.toString();
        return res;
    }
}

作者:LeetcodeFrom20210207
链接:https://leetcode-cn.com/problems/simplify-path/solution/71-jian-hua-lu-jing-by-leetcodefrom20210-c9uv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

逆波兰表达式求值(medium难度)

https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/

class Solution {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<Integer>();
        int n = tokens.length;
        for (int i = 0; i < n; i++) {
            String token = tokens[i];
            if (isNumber(token)) {
                stack.push(Integer.parseInt(token));
            } else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (token) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                    default:
                }
            }
        }
        return stack.pop();
    }

    public boolean isNumber(String token) {
        return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/solution/ni-bo-lan-biao-da-shi-qiu-zhi-by-leetcod-wue9/
来源:力扣(LeetCode)

class Solution {
    public int evalRPN(String[] tokens) {
        int n = tokens.length;
        int[] stack = new int[(n + 1) / 2];
        int index = -1;
        for (int i = 0; i < n; i++) {
            String token = tokens[i];
            switch (token) {
                case "+":
                    index--;
                    stack[index] += stack[index + 1];
                    break;
                case "-":
                    index--;
                    stack[index] -= stack[index + 1];
                    break;
                case "*":
                    index--;
                    stack[index] *= stack[index + 1];
                    break;
                case "/":
                    index--;
                    stack[index] /= stack[index + 1];
                    break;
                default:
                    index++;
                    stack[index] = Integer.parseInt(token);
            }
        }
        return stack[index];
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/solution/ni-bo-lan-biao-da-shi-qiu-zhi-by-leetcod-wue9/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值