栈与队列

  1. 队列实现栈:https://leetcode-cn.com/problems/implement-stack-using-queues/

    class MyStack {
    
     Queue<Integer> queue = new LinkedList<>();
    
     public MyStack() {
     }
    
     public void push(int x) {
         queue.add(x);
     }
    
     public int pop() {
         //头尾倒置
         for(int i=0;i<queue.size()-1;i++){
             queue.add(queue.poll());
         }
         return queue.poll();
     }
    
     public int top() {
         //头尾倒置
         for(int i=0;i<queue.size()-1;i++){
             queue.add(queue.poll());
         }
         int result = queue.poll();
         queue.add(result);
         return result;
     }
    
     public boolean empty() {
         return queue.isEmpty();
     }
    }
  2. 栈实现队列:https://leetcode-cn.com/problems/implement-queue-using-stacks/

    class MyQueue {
    
     Stack<Integer> stack1 = new Stack<>();
     Stack<Integer> stack2 = new Stack<>();
     public MyQueue() {
     }
    
     public void push(int x) {
         stack1.push(x);
     }
    
     public int pop() {
         if(stack2.isEmpty()){
             while(!stack1.isEmpty()){
                 stack2.push(stack1.pop());
             }
         }
         if(stack2.isEmpty()){
             return -1;
         }else{
             return stack2.pop();
         }
     }
    
     public int peek() {
         if(stack2.isEmpty()){
             while(!stack1.isEmpty()){
                 stack2.push(stack1.pop());
             }
         }
         if(stack2.isEmpty()){
             return -1;
         }else{
             return stack2.peek();
         }
     }
    
     public boolean empty() {
         return stack1.isEmpty() && stack2.isEmpty();
     }
    }
  3. 最小栈:https://leetcode-cn.com/problems/min-stack/

    class MinStack {
    
     Stack<Integer> stack = new Stack<>();
     Stack<Integer> min = new Stack<>();
    
     public MinStack() {
     }
    
     public void push(int x) {
         stack.push(x);
         //min栈顶维护最小值
         if(min.isEmpty() || min.peek() >= x){
             min.push(x);
         }
     }
    
     public void pop() {
         if(stack.pop().equals(min.peek())){
             min.pop();
         }
     }
    
     public int top() {
         return stack.peek();
     }
    
     public int getMin() {
         return min.peek();
     }
    }
  1. 队列的最大值:https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/

    class MaxQueue {
    
     LinkedList<Integer> queue = new LinkedList<>();
     LinkedList<Integer> max = new LinkedList<>();
     public MaxQueue() {
     }
    
     public int max_value() {
         if(queue.isEmpty()){
             return -1;
         }else{
             return max.peekFirst();
         }
     }
    
     public void push_back(int value) {
         queue.addLast(value);
         //max队列的队尾维护最大值,先弹出再存入
         while(!max.isEmpty() && max.peekLast() <= value){
             max.pollLast();
         }
         max.addLast(value);
     }
    
     public int pop_front() {
         if(queue.isEmpty()){
             return -1;
         }
         int result = queue.pollFirst();
         if(max.peekFirst().equals(result)){
             max.pollFirst();
         }
         return result;
     }
    }
  1. 有效括号:https://leetcode-cn.com/problems/valid-parentheses/
    class Solution {
     public boolean isValid(String s) {
         //单调栈,栈存入右半,如果栈为空或者栈顶不等于下一个字符,则无法闭合,返回false
         Stack<Character> stack = new Stack<>();
         char[] str = s.toCharArray();
         for(char c : str){
             if(c == '('){
                 stack.push(')');
             }else if(c == '['){
                 stack.push(']');
             }else if(c == '{'){
                 stack.push('}');
             }else if(stack.isEmpty() || stack.pop() != c){
                 return false;
             }
         }
         //返回栈是否为空
         return stack.isEmpty();
     }
    }
  1. 最长有效括号:https://leetcode-cn.com/problems/longest-valid-parentheses/

    class Solution {
     public int longestValidParentheses(String s) {
         Stack<Integer> stack = new Stack<>();
         //预存-1,用于匹配第一个字符为右半括号的情况
         stack.push(-1);
         char[] str = s.toCharArray();
    
         int result = 0;
         for(int i=0;i<str.length;i++){
             if(str[i] == '('){
                 stack.push(i);
             }else{
                 //匹配,弹出栈顶
                 stack.pop();
                 //如果栈顶弹出后栈为空,从当前位置重新统计,否则计算长度
                 if(stack.isEmpty()){
                     stack.push(i);
                 }else{
                     result = Math.max(result,i-stack.peek());
                 }
             }
         }
         return result;
     }
    }
  2. 柱状图中最大矩形:https://leetcode-cn.com/problems/largest-rectangle-in-histogram/

    class Solution {
     public int largestRectangleArea(int[] heights) {
    
         //复制一个数组,在头尾插入高度为0的柱体
         int[] temp = new int[heights.length+2];
         System.arraycopy(heights,0,temp,1,heights.length);
    
         Stack<Integer> stack = new Stack<>();
         int result = 0;
         for(int i=0;i<temp.length;i++){
             //找到第一个比栈顶小的元素
             while(!stack.isEmpty() && temp[stack.peek()] > temp[i]){
                 //提取栈顶为高
                 int high = temp[stack.pop()];
                 int width = i - stack.peek()-1;
                 result = Math.max(result,high*width);
             }
             stack.push(i);
         }
         return result;
     }
    }
  3. 每日温度:https://leetcode-cn.com/problems/daily-temperatures/

    class Solution {
     public int[] dailyTemperatures(int[] T) {
         Stack<Integer> stack = new Stack<>();
         int[] result = new int[T.length];
    
         for(int i=0;i<T.length;i++){
             //当前元素比栈顶大时,提取栈顶作为索引,计算天数差值
             while(!stack.isEmpty() && T[stack.peek()] < T[i]){
                 int index = stack.pop();
                 result[index] = i - index;
             }
             stack.push(i);
         }
         return result;
     }
    }
  4. 滑动窗口最大值:https://leetcode-cn.com/problems/sliding-window-maximum/

    class Solution {
     public int[] maxSlidingWindow(int[] nums, int k) {
         LinkedList<Integer> queue = new LinkedList<>();
         //窗口数组
         int[] result = new int[nums.length - k + 1];
         int index = 0;
    
         for(int i=0;i<nums.length;i++){
             while(!queue.isEmpty() && nums[queue.peekLast()] < nums[i]){
                 queue.pollLast();
             }
             queue.addLast(i);
    
             //如果左边界i-k == queue.peekFirst(),说明要右移,弹出队首
             if(i-k == queue.peekFirst()){
                 queue.pollFirst();
             }
             //如果右边界i >= k-1,要存入
             if(i >= k-1){
                 result[index++] = nums[queue.peekFirst()];
             }
         }
         return result;
     }
    }
  1. 栈的弹出和压入:https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/

  2. 二叉搜索树的后序遍历序列:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/

全部评论

相关推荐

从大一开始就开始学习Java,一路走来真的不算容易,每次面试都被压力,不过这次终于达成了自己的一大心愿!时间线和面经:8.17-投递9.1-一面实习+项目拷打看门狗机制讲一下redis加锁解锁的本身操作是什么Lua脚本是干什么的udp和tcp讲一下流量控制讲一下令牌桶算法说一下大端和小端是什么线程和协程有什么区别怎么切换协程切换的时候具体做了什么对于程序来说,你刚才提到的保存和恢复现场,这个现场有哪些信息udp优势现在有一个客户端和服务端,要实现TCP的通信,我们的代码要怎么写服务器怎么感知有新的连接怎么处理多个客户端的请求连接TCP怎么处理粘包和分包现在有两个文件,然后每个文件都有一亿条URL,每个的长度都很长,要怎么快速查找这两个文件共有的URLHashmap底层说一下怎么尽量提升插入和查询的效率如果要查找快,查询快,还有解决非空的问题,怎么做LoadingCache了解吗手撕:堆排序9.4-二面部门的leader,超级压力面拷打实习+项目,被喷完全没东西类的加载到垃圾回收整个底层原理讲一遍类加载谁来执行类加载器是什么东西,和进程的关系Java虚拟机是什么东西,和进程的关系如果我们要执行hello&nbsp;world,那虚拟机干了什么呢谁把字节码翻译成机器码,操作时机是什么Java虚拟机是一个执行单元吗Java虚拟机和操作系统的关系到底什么,假如我是个完全不懂技术的人,举例说明让我明白一个操作系统有两个Java程序的话,有几个虚拟机有没有单独的JVM进程存在启动一个hello&nbsp;world编译的时候,有几个进程JVM什么时候启动比如执行一条Java命令的时候对应一个进程,然后这个JVM虚拟机到底是不是在这个进程里面,还是说要先启动一个JVM虚拟机的进程垃圾回收机制的时机能手动触发垃圾回收吗垃圾回收会抢占业务代码的CPU吗垃圾回收算法简单说说垃圾回收机制的stop&nbsp;the&nbsp;world存在于哪些时机垃圾回收中的计算Region的时候怎么和业务代码并行执行假如只有一个线程,怎么实现并行Java为什么要这么实现Java效率比C++慢很多,那为什么还要这样实现Java虚拟机到底是什么形式存在的说一下Java和C++的区别还有你对Java设计理念的理解无手撕面试结束的时候,我真的汗流浃背了,面试官还和我道歉,说他是故意压力面想看看我的反应的,还对我给予了高度评价:我当面试官这么多年,你是我见过最好的一个9.9-三面临时通知的加面,就问了三十分钟项目9.11-hr面问过往经历,未来计划,想从腾讯实习中得到什么?当场告知leader十分满意我,所以直接ochr面完一分钟官网流程变成录用评估中,30分钟后mt加微信告知offer正在审批9.15-offer这一次腾讯面试体验真的不错,每个面试官能感觉到专业能力很强,反馈很足,比起隔壁某节真是好太多以后就是鹅孝子了
三本咋了:当面试官这么多年你是我见过的最好的一个
你面试被问到过哪些不会的...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务