首页 > 其他分享 >[剑指offer] 队列&栈篇

[剑指offer] 队列&栈篇

时间:2023-09-17 20:55:06浏览次数:32  
标签:offer 队列 栈篇 int static new push Stack public

JZ9 用两个栈实现队列

 1 /* 模拟入队 */
 2 public class JZ9_1
 3 {
 4     public static Stack<Integer> stack1 = new Stack<Integer>();
 5     public static Stack<Integer> stack2 = new Stack<Integer>();
 6 
 7     public static void push(int node)
 8     {
 9         if (stack1.isEmpty())
10         {
11             while (!stack2.isEmpty())
12                 stack1.push(stack2.pop());
13             stack2.push(node);
14             while (!stack1.isEmpty())
15                 stack2.push(stack1.pop());
16         }
17     }
18 
19     public static int pop()
20     {
21         return stack2.pop();
22     }
23 }
24 
25 /* ⭐模拟出队⭐ */
26 public class JZ9_2
27 {
28     public static Stack<Integer> stack1 = new Stack<Integer>();
29     public static Stack<Integer> stack2 = new Stack<Integer>();
30 
31     public static void push(int node)
32     {
33         stack1.push(node);
34     }
35 
36     public static int pop()
37     {
38         if (stack2.empty())
39             while (!stack1.empty())
40                 stack2.push(stack1.pop());
41         return stack2.pop();
42     }
43 }

JZ30 包含min函数的栈⭐

 1 /*
 2 * stack: 正常保存每一个入栈的数字
 3 * minStack: 当数字入栈后,保存栈当前状态最小的数
 4 * */
 5 public class JZ30_1
 6 {
 7     public static Stack<Integer> stack = new Stack<>();
 8     public static Stack<Integer> minStack = new Stack<>();
 9 
10     public static void push(int node)
11     {
12         stack.push(node);
13         if (minStack.isEmpty() || node < minStack.peek())
14             minStack.push(node);
15         else
16             minStack.push(minStack.peek());
17     }
18 
19     public static void pop()
20     {
21         stack.pop();
22         minStack.pop();
23     }
24 
25     public static int top()
26     {
27         return stack.peek();
28     }
29 
30     public static int min()
31     {
32         return minStack.peek();
33     }
34 }
35 
36 /*
37  * stack: 正常保存每一个入栈的数字
38  * minStack: 最小元素每一次都进栈,造成重复,所以可以优化成只有当进栈元素≤目前最小元素时,进栈元素才放入最小栈
39  * */
40 public class JZ30_2
41 {
42     public static Stack<Integer> stack = new Stack<>();
43     public static Stack<Integer> minStack = new Stack<>();
44 
45     public static void push(int node)
46     {
47         stack.push(node);
48         if (minStack.isEmpty() || node <= minStack.peek())
49             minStack.push(node);
50     }
51 
52     public static void pop()
53     {
54         if (stack.peek() <= minStack.peek())
55             minStack.pop();
56         stack.pop();
57     }
58 
59     public static int top()
60     {
61         return stack.peek();
62     }
63 
64     public static int min()
65     {
66         return minStack.peek();
67     }
68 }

JZ31 栈的压入、弹出序列

 1 /* 辅助栈 */
 2 public class JZ31_1
 3 {
 4     public static boolean IsPopOrder(int[] pushV, int[] popV)
 5     {
 6         Stack<Integer> sta = new Stack<>();
 7         int idx = 0;
 8         for (int i = 0; i < popV.length; i++)
 9         {
10             while (sta.isEmpty() || sta.peek() != popV[i])
11             {
12                 if (idx == pushV.length) return false;
13                 sta.push(pushV[idx++]);
14             }
15             sta.pop();
16         }
17         return sta.isEmpty();
18     }
19 }
20 
21 /* ⭐原地栈⭐ */
22 public class JZ31_2
23 {
24     public static boolean IsPopOrder(int[] pushV, int[] popV)
25     {
26         int stackTop = -1, idx = 0;
27         for (int i = 0; i < pushV.length; i++)
28         {
29             pushV[++stackTop] = pushV[i];
30             while (stackTop >= 0 && pushV[stackTop] == popV[idx])
31             {
32                 idx++;
33                 stackTop--;
34             }
35         }
36         return idx == popV.length;
37     }
38 }

JZ73 翻转单词序列

 1 /* 字符串翻转 */
 2 public class JZ73_1
 3 {
 4     public static String ReverseSentence(String str)
 5     {
 6         StringBuilder res = new StringBuilder();
 7         String[] s = str.split(" ");
 8         for (int i = s.length - 1; i >= 0; i--)
 9             res.append(s[i] + (i != 0 ? " " : ""));
10         return new String(res);
11     }
12 }
13 
14 /* 滑动窗口 */
15 public class JZ73_2
16 {
17     public static String ReverseSentence(String str)
18     {
19         int left = 0, right = 0;
20         StringBuilder res = new StringBuilder();
21         StringBuilder rev_str = new StringBuilder(str).reverse();
22         while (true)
23         {
24             if (right == rev_str.length() || rev_str.charAt(right) == ' ')
25             {
26                 res.append(new StringBuilder(rev_str.substring(left, right)).reverse());
27                 left = right + 1;
28                 if (right == rev_str.length())
29                     break;
30                 res.append(" ");
31             }
32             right++;
33         }
34         return res.toString();
35     }
36 }
37 
38 /* 栈 */
39 public class JZ73_3
40 {
41     public static String ReverseSentence(String str)
42     {
43         Stack<String> sta = new Stack<String>();
44         StringBuilder res = new StringBuilder();
45         String[] temp = str.split(" ");
46         for (int i = 0; i < temp.length; i++)
47             sta.push(temp[i] + (i == 0 ? "" : " "));
48         while (!sta.isEmpty())
49             res.append(sta.pop());
50         return res.toString();
51     }
52 }

JZ59 滑动窗口的最大值⭐

 1 /* 单调队列 */
 2 public class JZ59_1
 3 {
 4     public static ArrayList<Integer> maxInWindows(int[] num, int size)
 5     {
 6         ArrayList<Integer> res = new ArrayList<>();
 7         Deque<Integer> deque = new LinkedList<>();
 8         if (size == 0) return res;
 9         for (int i = 0; i < num.length; i++)
10         {
11             while (!deque.isEmpty() && num[deque.getLast()] < num[i])
12                 deque.removeLast();
13             deque.addLast(i);
14             if (i - deque.getFirst() >= size) deque.removeFirst();
15             if (i >= size - 1) res.add(num[deque.getFirst()]);
16         }
17         return res;
18     }
19 }

标签:offer,队列,栈篇,int,static,new,push,Stack,public
From: https://www.cnblogs.com/VividBinGo/p/17709797.html

相关文章

  • 消息队列中如何保证消息的顺序性?
    面试官心理分析其实这个也是用MQ的时候必问的话题,第一看看你了不了解顺序这个事儿?第二看看你有没有办法保证消息是有顺序的?这是生产系统中常见的问题。面试题剖析我举个例子,我们以前做过一个mysql binlog 同步的系统,压力还是非常大的,日同步数据要达到上亿,就是说数据从一个mys......
  • k8s限速队列
    channel问题channel是go协程间通信的主要方式。channel预设容量,很难评估,不支持动态扩容。k8s的client-go提供了基于切片的线程安全的并发队列,解耦生产者与消费者,提供了去重、限速、重试加入队列等功能。k8scontroller处理事件官方例子生产者//创建一个workqueuequeue:=w......
  • 二维单调队列
    单调队列通常用来解决区间最值问题。二维单调队列用来在矩阵中找子矩阵的最值,即求a*b大小的子矩阵中的最大值与最小值。做法:我们先预处理出每行滑动窗口长度为b的最值,并将其放到窗口最右侧位置;如0~b-1窗口的最值放到下标为b-1的位置。处理完行后,我们对列进行处理......
  • [剑指offer] 回溯篇
    JZ12矩阵中的路径1/*DFS*/2publicclassJZ12_13{4publicstaticboolean[][]vis;5publicstaticint[]dx=newint[]{-1,1,0,0};6publicstaticint[]dy=newint[]{0,0,-1,1};78publicstaticbooleanhasPath(char[][]......
  • [剑指offer] 模拟篇
    JZ29 顺时针打印矩阵1/*模拟*/2publicclassJZ29_13{4publicstaticArrayList<Integer>printMatrix(int[][]matrix)5{6ArrayList<Integer>res=newArrayList<>();7intleft=0,right=matrix[0].length-......
  • 深入研究消息队列06 高级功能
    27Topic分区订阅如何实现动态配置在消息队列里面,因为需要保持架构的简洁度,基于本地文件也是一种常用的方案。比如Kafka和Pulsar就是基于ZooKeeper来实现的动态配置,因为架构中已经集成了ZooKeeper。RocketMQ的Nameserver是一个缓存组件,没有实际的存储和Watch机制,无法......
  • 代码随想录算法训练营第10天| 232.用栈实现队列 ● 225. 用队列实现栈
    栈和队列232.用栈实现队列stack:queue:卡哥代码一个入栈,一个出栈,即可模拟队列的pop操作pop之前要检查出栈是否为空若为空,则排出入栈里所有的元素至出栈中classMyQueue{public:stack<int>stackIn;stack<int>stackOut;MyQueue(){......
  • 3 - 任务调度算法 & 同步与互斥 &队列
    之前的都是按照优先级不同允许抢占(不讲道理),不管你在做什么,轮到优先级最高的任务,直接抢占执行怎样才能讲道理呢?稍微等等嘛,等我做完活你再做 1支持抢占,0不支持抢占 同优先级任务是否交替执行,1交替0不交 空闲任务是否礼让其他任务礼让的话,自己的函数逻辑在时间片内只执行......
  • POJ 2823 Sliding Window 单调队列
    这道题就是用单调队列来维护,但是用G++交TLE,用c++5000多ms,真是囧...代码很丑,就凑合着看吧#include<stdio.h>inta[1000009],que[1000009];intmain(){ intn,k,i,head,tail,flag=1,f; scanf("%d%d",&n,&k); for(i=1;i<=n;i++) scanf("%d",&a[i]); head=......
  • [剑指offer] 链表篇
    JZ6从尾到头打印链表1/*从尾到头递归*/2publicclassJZ6_13{4privatestaticArrayList<Integer>res=newArrayList<>();56publicstaticArrayList<Integer>printListFromTailToHead(ListNodelistNode)7{8......