栈和队列

  • 面试题

1.题目描述

给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

题目分析

在这里插入图片描述

程序代码

import java.util.Deque;
import java.util.LinkedList;


class Solution {
    public boolean compareBracket(char left,char right) {
        if(left == '(' &&right == ')')  {
            return true;
        }
        if(left == '[' &&right == ']')  {
            return true;
        }
        if(left == '{' &&right == '}')  {
            return true;
        }
        return false;
    }
    public boolean isValid(String s) {
        //1.准备好一个栈
        Deque<Character> stack = new LinkedList<>();

        //遍历1
//        for (int i = 0; i < s.length(); i++) {
//            char c = s.charAt(i);
//        }
        //遍历2
        char[] charArray = s.toCharArray();
        for(char c : charArray) {
            switch (c) {
                case '(':
                case '[':
                case '{':
                    stack.push(c);
                    break;
                default:{
                    if(stack.isEmpty()) {
                        return false;
                    }
                    char left = stack.pop();
                    if(!compareBracket(left,c)) {
                        return false;
                    }
                }
            }
        }
        if(stack.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}

2.用队列实现栈

使用队列实现栈的下列操作:

push(x) -- 元素 x 入栈
pop() -- 移除栈顶元素
top() -- 获取栈顶元素
empty() -- 返回栈是否为空

程序代码

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> queue;

    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue.add(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        int size = queue.size();
        for(int i = 0;i < size - 1;i++) {
            Integer e = queue.remove();
            queue.add(e);
        }
        return queue.remove();
    }
    
    /** Get the top element. */
    public int top() {
        int size = queue.size();
        for(int i = 0;i < size - 1;i++) {
            Integer e = queue.remove();
            queue.add(e);
        }
        Integer t = queue.remove();
        queue.add(t);
        return t;
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}

3.用栈实现队列

使用栈实现队列的下列操作:

push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。

程序代码

class MyQueue {

    private Deque<Integer> s1;//准备出的
    private Deque<Integer> s2;//优先放的
    /** Initialize your data structure here. */
    public MyQueue() {
        s1 = new LinkedList<>();
        s2 = new LinkedList<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        s2.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(s1.isEmpty()) {
            while (!s2.isEmpty()) {
                Integer e = s2.pop();
                s1.push(e);
            }
        }
        return s1.pop();
    }

    /** Get the front element. */
    public int peek() {
        if(s1.isEmpty()) {
            while (!s2.isEmpty()) {
                Integer e = s2.pop();
                s1.push(e);
            }
        }
        return s1.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }
}

4.实现一个最小栈

设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。

push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。

程序代码

class MinStack {
    Deque<Integer> xStack;
    Deque<Integer> minStack;

    public MinStack() {
        xStack = new LinkedList<Integer>();
        minStack = new LinkedList<Integer>();
        minStack.push(Integer.MAX_VALUE);
    }
    
    public void push(int x) {
        xStack.push(x);
        minStack.push(Math.min(minStack.peek(), x));
    }
    
    public void pop() {
        xStack.pop();
        minStack.pop();
    }
    
    public int top() {
        return xStack.peek();
    }
    
    public int getMin() {
        return minStack.peek();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值