栈和队列的方法总结及使用

栈(Stack):后进先出(LIFO)

方法解释
push(e)压栈
pop()出栈
peek()取栈顶元素(不删除)
boolean empty()判断栈是否为空

队列(Queue):先进先出(FIFO)

方法解释
add(e) / ofter(e)入队列,add()抛出异常,offer()返回特殊值
remove() / pool()出队列,remove()抛出异常,offer()返回特殊值
element() / peek()取队首元素(不删除)element()抛出异常,peek()返回特殊值
isEmpty()判断是否为空


练习:

力扣(LeetCode) 20. 有效的括号
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

class Solution {
    public static boolean isValid(String s) {
       Stack<Character> stack = new Stack<Character>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length();i++) {

            switch (chars[i]) {
                case '(':
                case '{':
                case '[':
                    stack.push(chars[i]); //放入栈
                    break;
                default:
                    if (stack.isEmpty()) {
                        return false;
                    }
                    //取出栈顶元素
                    char left = stack.pop();
                    if (!compareBracket(left, chars[i])) {
                        return false;
                    }
            }
        }
        if (!stack.isEmpty() ) {
            return false;
        } else {
            return true;
        }
    }

    private static boolean compareBracket(char left, char right) {
        if (left == '(' && right == ')') {
            return true;
        }
        if (left == '{' && right == '}') {
            return true;
        }
        if (left == '[' && right == ']') {
            return true;
        }
        return false;
    }
}

力扣(LeetCode)232. 用栈实现队列

import java.util.Stack;

class MyQueue {

    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    /** Initialize your data structure here. */
    public MyQueue() {
        stack1 = new Stack<Integer>(); //取
        stack2 = new Stack<Integer>(); //放
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack2.push(x); //往stack2中放元素
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (stack1.isEmpty()) {
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
            }
        }
        return stack1.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (stack1.isEmpty()) {
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
            }
        }
        return stack1.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty() && stack2.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();
 */

力扣(LeetCode)225.用队列实现栈

import java.util.Stack;

class MyQueue {

    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    /** Initialize your data structure here. */
    public MyQueue() {
        stack1 = new Stack<Integer>(); //取
        stack2 = new Stack<Integer>(); //放
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack2.push(x); //往stack2中放元素
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (stack1.isEmpty()) {
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
            }
        }
        return stack1.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (stack1.isEmpty()) {
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
            }
        }
        return stack1.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty() && stack2.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();
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值