LeetCode-栈和队列专题

1.用栈实现队列

  1. Implement Queue using Stacks
    Leetcode

用栈实现队列,因为栈是先进后出的一种数据结构,需要两个栈互相辅助才可以实现队列
in队列负责将元素压到栈中
out队列负责将元素颠倒过来压到栈中
这样就可以用栈实现一个队列了

//2020年10月12日
class MyQueue {

    private Stack<Integer> in = new Stack<>();
    private Stack<Integer> out = new Stack<>();

    /** Initialize your data structure here. */
    public MyQueue() {

    }

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

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        inToOut();
        return out.pop();
    }

    /** Get the front element. */
    public int peek() {
        inToOut();
        return out.peek();
    }

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

    public void inToOut(){
        if(out.isEmpty()){
            while(!in.isEmpty()){
                out.push(in.pop());
            }
        }
    }
}

2.用队列实现栈

  1. Implement Stack using Queues
    Leetcode

用队列实现栈,因为队列是先进先出的一种数据结构
压栈时需要先将当前元素插入,再将之前插入的元素依次出队入队
这样就可以用队列实现一个栈了

//2020年10月12日
class MyStack {

    Queue<Integer> queue;

    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue.offer(x);
        int count = queue.size();
        while(count-- > 1){
            queue.offer(queue.poll());
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }

    /** Get the top element. */
    public int top() {
        return queue.peek();
    }

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

3.最小值栈

  1. min-stack
    Leetcode

主要思路就是创建一个栈的同时再创建一个记录当前栈最小值的栈

//2020年10月13日
class MinStack {

    Stack<Integer> s = new Stack<>();
    Stack<Integer> min = new Stack<>();

    /** initialize your data structure here. */
    public MinStack() {

    }

    public void push(int x) {
        s.push(x);
        if(min.isEmpty()){
            min.push(x);
        }else{
            if(min.peek() < x){
                min.push(min.peek());
            }else{
                min.push(x);
            }
        }
    }

    public void pop() {
        s.pop();
        min.pop();
    }

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

    public int getMin() {
        return min.peek();
    }
}

4.用栈实现括号匹配

  1. Valid Parentheses(easy)
    Leetcode

这题我一开始还没想明白,看了一眼解析后明白了
要想实现匹配,左括号和右括号的数量是相等的
而且字符串的一开始不能是右括号

//2020年10月13日
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(char c : s.toCharArray()){
            if(c == '(' || c == '{' || c == '['){
                stack.push(c);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                char popChar = stack.pop();
                boolean b1 = c == ')' && popChar != '(';
                boolean b2 = c == ']' && popChar != '[';
                boolean b3 = c == '}' && popChar != '{';
                if(b1 || b2 || b3){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}

5.每日温度

  1. Daily Temperatures (Medium)
    Leetcode

解法1,双重循环

//2020年10月19日
class Solution {
    public int[] dailyTemperatures(int[] T) {
        int a[] = new int[T.length];
        for(int i=0;i<T.length;i++){
            int count = 0;
            for(int j=i+1;j<T.length;j++){
                if(T[j] > T[i]){
                    a[i] = count + 1;
                    break;
                }else if (T[j] <= T[i]){
                    count++;
                }
            }
        }
        return a;
    }
}

解法2,利用栈

class Solution {
    public int[] dailyTemperatures(int[] T) {
        int a[] = new int[T.length];
        Stack<Integer> s = new Stack<>();
        for(int i=0;i<T.length;i++){
            while(!s.isEmpty()&&T[i] > T[s.peek()]){
                int temp = s.pop();
                a[temp] = i - temp;
            }
            s.add(i);
        }
        return a;
    }
}
全部评论

相关推荐

会飞的猿:我看你想进大厂,我给你总结一下学习路线吧,java语言方面常规八股要熟,那些java的集合,重点背hashmap八股吧,jvm类加载机制,运行时分区,垃圾回收算法,垃圾回收器CMS、G1这些,各种乐观锁悲观锁,线程安全,threadlocal这些。在进阶一些的比如jvm参数,内存溢出泄漏排查,jvm调优。我这里说的只是冰山一角,详细八股可以去网上找,这不用去买,都免费资源。mysql、redis可以去看小林coding,我看你简历上写了,你一定要熟,什么底层b+树、索引结构、innodb、mvcc、undo log、redo log、行级锁表级锁,这些东西高频出现,如果面试官问我这些我都能笑出来。消息队列rabbitmq也好kafka也好,学一种就行,什么分区啊副本啊确认机制啊怎么保证不重复消费、怎么保证消息不丢失这些基本的一定要会,进阶一点的比如LEO、高水位线、kafka和rocketmq底层零拷贝的区别等等。计算机网络和操作系统既然你是科班应该理解起来问题不大,去看小林coding这两块吧,深度够了。spring boot的八股好好看看吧,一般字节腾讯不这么问,其他的java大厂挺爱问的,什么循环依赖啥的去网上看看。数据结构的话科班应该问题不大,多去力扣集中突击刷题吧。项目的话其实说白了还是结合八股来,想一想你写的这些技术会给你挖什么坑。除此之外,还有场景题、rpc、设计模式、linux命令、ddd等。不会的就别往简历上写了,虽然技术栈很多的话好看些,但背起来确实累。总结一下,多去实习吧,多跳槽,直到跳到一个不错的中厂做跳板,这是一条可行的进大厂的路线。另外,只想找个小厂的工作的话,没必要全都照这些准备,太累了,重点放在框架的使用和一些基础八股吧。大致路线就这样,没啥太多难度,就是量大,你能达到什么高度取决于你对自己多狠,祝好。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务