前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【数据算法与结构】栈与队列篇

【数据算法与结构】栈与队列篇

作者头像
xxxflower
发布2024-03-11 08:26:22
780
发布2024-03-11 08:26:22
举报
文章被收录于专栏:《数据结构》

20. 有效的括号

20. 有效的括号

代码语言:javascript
复制
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);
            // 如果是左括号就入栈
            if(ch=='(' || ch=='[' || ch=='{'){
                stack.push(ch);
            }else{
                // 如果是右括号就进行匹配
                // 如果是右括号栈为空了则不是有效的括号
                if(stack.empty()){
                    return false;
                }else{
                    // 是右括号且匹配成功了就弹出
                    char ch2=stack.peek();
                    if(ch2=='('&&ch==')' || ch2=='['&&ch==']' || ch2=='{'&&ch=='}' ){
                        stack.pop();
                    }else{
                        return false;
                    }
                }
            }
        }
        // 字符串遍历完成但是栈不为空则不是有效括号
        if(!stack.empty()){
            return false;
        }
        return true;
    }
}

150. 逆波兰表达式求值

150. 逆波兰表达式求值

代码语言:javascript
复制
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x:tokens){
           if(x.equals("+") || x.equals("-") ||x.equals("*") ||x.equals("/")){
               int num1 = stack.pop();
               int num2 = stack.pop();
               switch(x){
                   case "+":
                   stack.push(num2 + num1);
                   break;
                   case "-":
                   stack.push(num2 - num1);
                   break;
                   case "*":
                   stack.push(num2 * num1);
                   break;
                   case "/":
                   stack.push(num2 / num1);
                   break;
               }
           } else{
               stack.push(Integer.parseInt(x));
           }
        }
        return stack.pop();
    }
}

JZ31 栈的压入、弹出序列

JZ31 栈的压入、弹出序列

思路: pushV中的元素依次入栈,和popV中的元素比较,如果相同就出栈,遍历结束后,如果栈为空,那么就说明是正确的弹出顺序.

代码:

代码语言:javascript
复制
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pushV int整型一维数组 
     * @param popV int整型一维数组 
     * @return bool布尔型
     */
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j=0;
        for(int i=0;i<pushV.length;i++){
            stack.push(pushV[i]);
            while(j<popV.length && !stack.empty() && stack.peek()==popV[j]){
                stack.pop();
                j++;
            } 
        }
        if(stack.isEmpty()){
            return true;
        }
        return false;
    }
}

622. 设计循环队列

622. 设计循环队列

代码语言:javascript
复制
class MyCircularQueue {
    public int[] elem;
    public int front;
    public int rear;

    public MyCircularQueue(int k) {
        elem = new int[k+1];    
    }
    
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        elem[rear] = value;
        rear = (rear+1) % elem.length;
        return true;  
    }
    
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        front = (front+1) % elem.length;
        return true;
    }
    
    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }
    
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int index = (rear==0?elem.length-1:rear-1);
        return elem[index];
    }
    
    public boolean isEmpty() {
        if(front==rear){
            return true;
        }
        return false;
    }
    
    public boolean isFull() {
        if((rear+1) % elem.length == front){
            return true;
        }
        return false;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */

225. 用队列实现栈

225. 用队列实现栈

代码语言:javascript
复制
class MyStack {
    private Queue<Integer> q1;
    private Queue<Integer> q2;
    public MyStack() {
        q1 = new LinkedList<Integer>();
        q2 = new LinkedList<Integer>();
    }
    
    public void push(int x) {
        if(!q1.isEmpty()){
            q1.offer(x);
        }else if(!q2.isEmpty()){
            q2.offer(x);
        }else{
            q1.offer(x);
        }
    }
    
    public int pop() {
        if(empty()){
            return -1;
        }
        if(!q1.isEmpty()){
            int size=q1.size();
            for(int i=0;i<size-1;i++){
                q2.offer(q1.poll());
            }
            return q1.poll();
        }else{
            int size = q2.size();
            for(int i=0;i<size-1;i++){
                q1.offer(q2.poll());
            }
            return q2.poll();
        }
    }
    
    public int top() {
        if(empty()){
            return -1;
        }
        if(!q1.isEmpty()){
            int size=q1.size();
            int ret=-1;
            for(int i=0;i<size;i++){
                ret = q1.poll();
                q2.offer(ret);
            }
            return ret;
        }else{
            int size = q2.size();
            int ret=-1;
            for(int i=0;i<size;i++){
                ret = q2.poll();
                q1.offer(ret);
            }
            return ret;
        }

    }
    
    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

232. 用栈实现队列

232. 用栈实现队列

代码语言:javascript
复制
class MyQueue {

    Stack<Integer> stackA;
    Stack<Integer> stackB;
    public MyQueue() {
        stackA = new Stack<>();
        stackB = new Stack<>();
    }
    
    public void push(int x) {
        stackA.push(x);
    }
    
    public int pop() {
        if(empty()){
            return -1;
        }
        if(stackB.empty()){
            while(!stackA.empty()){
                 stackB.push(stackA.pop());
            }  
        }
        return stackB.pop();
    }
    
    public int peek() {
        if(empty()){
            return -1;
        }
        if(stackB.empty()){
            while(!stackA.empty()){
                 stackB.push(stackA.pop());
            }  
        }
        return stackB.peek();
    }
    
    public boolean empty() {
        return stackA.isEmpty() && stackB.isEmpty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 20. 有效的括号
  • 150. 逆波兰表达式求值
  • JZ31 栈的压入、弹出序列
  • 622. 设计循环队列
  • 225. 用队列实现栈
  • 232. 用栈实现队列
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档