LeetCode栈与队列练习

一、常规小题

232. 用栈实现队列

一个栈肯定是实现不了啊,所以我们需要一个辅助栈。

class MyQueue {
    Stack<Integer> stack1, 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) {
        stack1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        f(stack1,stack2);
        return stack2.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        f(stack1,stack2);
        return stack2.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack2.empty() && stack1.empty();
    }
    
    private void f(Stack<Integer> s1, Stack<Integer> s2) {
       if(s2.empty()){
           while(!s1.empty()) {
                Integer temp = s1.pop();
                s2.push(temp);
            }
       }
    }
}

225. 用队列实现栈

当队中元素超过1,每进来一个,我们就把前n-1个元素从队尾在进一遍

class MyStack {

    private Queue<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.add(x);
        int cnt = queue.size();
        while (cnt-- > 1) {
            queue.add(queue.poll());
        }
    }

    public int pop() {
        return queue.remove();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}

155. 最小栈

使用一个辅助栈记录当前栈中的最小元素,要注意min要始终等于辅助栈栈顶元素确保入栈的正确。

class MinStack {
    private int min = Integer.MAX_VALUE;
    LinkedList<Integer> dataStack;
    LinkedList<Integer> minStack;
    /** initialize your data structure here. */
    public MinStack() {
        dataStack = new LinkedList<>();
        minStack = new LinkedList<>();
    }
    
    public void push(int x) {
        dataStack.add(x);
        min = Math.min(min,x);
        minStack.add(min);
    }
    
    public void pop() {
        dataStack.pollLast();
        minStack.pollLast();
        min = minStack.isEmpty() ? Integer.MAX_VALUE : minStack.peekLast();
    }
    
    public int top() {
        return dataStack.peekLast();
    }
    
    public int getMin() {
        return minStack.peekLast();
    }
}

 二、括号匹配问题

 20. 有效的括号

class Solution {
    public boolean isValid(String s) {
        if(s == "") return true;
        if(s == null || s.length()%2 == 1) return false;
        LinkedList<Character> stack = new LinkedList<>();
        for(int i = 0; i < s.length(); i++) {
            if(stack.isEmpty() || !f(stack.peekLast(),s.charAt(i))) {
                stack.add(s.charAt(i));
                continue;
            }
            if(f(stack.peekLast(),s.charAt(i))) {
                stack.pollLast();
            }
        }
        return stack.isEmpty();
    }
    private boolean f(char c1, char c2) {
        if(c1 == '('&&c2 == ')')
            return true;
        else if(c1 == '['&&c2 == ']')
            return true;
        else if(c1 == '{'&&c2 == '}')
            return true;
        else return false;
    }
}

394. 字符串解码

本题的难点在于我们要以入栈出栈的方式生成答案字符串。

用一个指针接收字符,用另外一个指针接收数字,当遇到一个左括号把字符和数字分别入栈,当遇到一个右括号先出栈数字进行翻倍,在出栈字符串拼接。

class Solution {
    public String decodeString(String s) {
        if(s == null) return null;
        if(s.length() == 0) return "";
        LinkedList<StringBuilder> q = new LinkedList<>();
        LinkedList<Integer> data = new LinkedList<>();
        StringBuilder ans = new StringBuilder();
        StringBuilder num = new StringBuilder();
        for(char c : s.toCharArray()) {
            if(c>='0'&&c<='9') num.append(c);
            else if(c>='a'&&c<='z') ans.append(c);
            else if(c>='A'&&c<='Z') ans.append(c);
            else if(c == '[') {
                data.add(Integer.valueOf(new String(num)));
                num = new StringBuilder();
                q.add(ans);
                ans = new StringBuilder();
            }else if(c == ']') {
                int n = data.pollLast();
                StringBuilder temp = new StringBuilder(ans);
                while(--n>0) {
                    ans.append(temp);
                }
                ans = q.pollLast().append(ans);
            }
        }
        return new String(ans);
    }
}

 三、找左右的第一个比本元素大或小的数

leetcod接雨水

posted @ 2020-03-15 22:01  卑微芒果  Views(232)  Comments(0Edit  收藏  举报