Leetcode-day09-栈和队列

232. 用栈实现队列

栈是先进后出,队列是先进先出。要用栈实现队列的入队,出队等操作,入队其实很简单,就是入栈就可以,主要是出队,这里可以用两个栈,来实现队列的先进先出。进栈放到in栈,出栈的时候先把in栈里面所有的元素都放到out栈里,然后对out栈出,就实现了队列的先进先出的效果。

 

class MyQueue {

    private Stack<Integer> in;
    private Stack<Integer> out;

    public MyQueue() {
        in = new Stack<>();
        out = new Stack<>();
    }
    
    public void push(int x) {
        in.push(x);
    }
    
    public int pop() {
        if(!out.isEmpty()){
            return out.pop();
        }else{
            while(!in.isEmpty()){
                out.push(in.pop());
            }
        }
        return out.pop();
    }
    
    public int peek() {
        int n = this.pop();
        out.push(n);
        return n;
    }
    
    public boolean empty() {
        return in.isEmpty()&&out.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();
 */

225. 用队列实现栈

其实用一个队列就可以实现栈,加入的时候直接加入队列,弹出的时候可以先把size-1的元素从队列里拿出来而且放到队列的末尾,这样拿出来的就是最后加入的元素,实现了后进先出。

这里主要是有一些队列的使用之前没有接触过,Queue是java中实现队列的接口,它总共只有6个方法,我们一般只用其中3个就可以了。Queue的实现类有LinkedList和PriorityQueue。最常用的实现类是LinkedList。

class MyStack {
    Queue<Integer> queue;
    
    public MyStack() {
        queue = new LinkedList<>();
    }
    
    public void push(int x) {
        queue.add(x);
    }
    
    public int pop() {
        rePosition();
        return queue.poll();
    }
    
    public int top() {
        rePosition();
        int result = queue.poll();
        queue.add(result);
        return result;
    }
    
    public boolean empty() {
        return queue.isEmpty();
    }

    public void rePosition(){
        int size = queue.size();
        size--;
        while(size-->0)
            queue.add(queue.poll());
    }
}

 20. 有效的括号

遇到左括号就放进栈里面,遇到右括号看能否找到匹配的左括号(如果匹配到右括号的时候栈已经空了,就说明失败),如果右括号匹配完之后栈里面还有元素,就说明失败

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                char top = stack.pop();
                if (!(c == ')' && top == '(' || c == ']' && top == '[' || c == '}' && top == '{')) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}

1047. 删除字符串中的所有相邻重复项

栈,遍历字符串的每个元素,如果该元素在栈中已经存在,就pop消除,不存在就入栈。最后放到StringBuilder里反转。

class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for(char c : s.toCharArray()){
            if(!stack.isEmpty()&&stack.peek()==c){
                stack.pop();
            }else{
                stack.push(c);
            }
        }
        StringBuilder sb = new StringBuilder();
        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }
}

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值