1. 用栈实现队列

232. Implement Queue using Stacks (Easy)

Leetcode / 力扣

class MyQueue {
Stack<Integer> stack1=new Stack<>();
Stack<Integer> stack2=new Stack<>(); /** Initialize your data structure here. */
public MyQueue() { } /** 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() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
} /** Get the front element. */
public int peek() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
} return stack2.peek();
} /** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty()&&stack2.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();
*/

2. 用队列实现栈

225. Implement Stack using Queues (Easy)

Leetcode / 力扣

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) {
int n=queue.size();
queue.add(x);
while(n-->0){
queue.add(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();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

3. 最小值栈

155. Min Stack (Easy)

Leetcode / 力扣

class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack; /** initialize your data structure here. */
public MinStack() {
stack=new Stack<>();
minStack=new Stack<>();
} public void push(int x) {
stack.push(x);
if(minStack.isEmpty()||minStack.peek()>=x){
minStack.add(x);
}
} public void pop() {
int val=stack.pop();
if(val==minStack.peek())minStack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return minStack.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

4. 用栈实现括号匹配

20. Valid Parentheses (Easy)

Leetcode / 力扣

class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
for(char c: s.toCharArray()){
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();
}
}

5. 数组中元素与下一个比它大的元素之间的距离

这个题目很巧妙,主要的用法在于是对栈存的元素是索引,并且是个单调栈。

739. Daily Temperatures (Medium)

Leetcode / 力扣

class Solution {
public int[] dailyTemperatures(int[] T) {
Stack<Integer> stack=new Stack<>();
int N=T.length;
int[] result=new int[N];
for(int i=0;i<N;i++){
while(!stack.isEmpty()&&T[i]>T[stack.peek()]){
int temp=stack.pop();
result[temp]=i-temp;
}
stack.push(i);
}
return result; }
}

6. 循环数组中比当前元素大的下一个元素'

也就是多了几行代码,全部设置为-1,循环的化用俩个数组

  • 思路
  • 1.将数组中所有元素全部置为-1
  • 2.遍历两次,相当于循环遍历
  • 3.第一遍遍历,入栈索引i
  • 4.只要后面元素比栈顶索引对应的元素大,索引出栈,更改res[sta.pop()]的数值
  • 5.最后栈里面剩余的索引对应的数组值,都为默认的-1(因为后面未找到比它大的值) */

503. Next Greater Element II (Medium)

Leetcode / 力扣

class Solution {
public int[] nextGreaterElements(int[] nums) {
int N=nums.length;
int[] res=new int[N];
Arrays.fill(res,-1);
Stack<Integer> stack=new Stack<>();
for(int i=0;i<N*2;i++){
int num=nums[i%N];
while(!stack.isEmpty()&&num>nums[stack.peek()]){
res[stack.pop()]=num;
}
if(i<N)stack.push(i);
}
return res;
}
}

Leedcode算法专题训练(栈和队列)的更多相关文章

  1. Leedcode算法专题训练(搜索)

    BFS 广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点.需要注意的是,遍历过的节点不能再次被遍历. 第一层: 0 -> {6,2,1,5} ...

  2. Leedcode算法专题训练(树)

    递归 一棵树要么是空树,要么有两个指针,每个指针指向一棵树.树是一种递归结构,很多树的问题可以使用递归来处理. 1. 树的高度 104. Maximum Depth of Binary Tree (E ...

  3. Leedcode算法专题训练(贪心)

    1. 分配饼干 455. 分发饼干 题目描述:每个孩子都有一个满足度 grid,每个饼干都有一个大小 size,只有饼干的大小大于等于一个孩子的满足度,该孩子才会获得满足.求解最多可以获得满足的孩子数 ...

  4. Leedcode算法专题训练(分治法)

    归并排序就是一个用分治法的经典例子,这里我用它来举例描述一下上面的步骤: 1.归并排序首先把原问题拆分成2个规模更小的子问题. 2.递归地求解子问题,当子问题规模足够小时,可以一下子解决它.在这个例子 ...

  5. Leedcode算法专题训练(二分查找)

    二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...

  6. Leedcode算法专题训练(排序)

    排序 快速排序 用于求解 Kth Element 问题,也就是第 K 个元素的问题. 可以使用快速排序的 partition() 进行实现.需要先打乱数组,否则最坏情况下时间复杂度为 O(N2). 堆 ...

  7. Leedcode算法专题训练(双指针)

    算法思想 双指针 167. 两数之和 II - 输入有序数组 双指针的典型用法 如果两个指针指向元素的和 sum == target,那么得到要求的结果: 如果 sum > target,移动较 ...

  8. Leedcode算法专题训练(位运算)

    https://www.cnblogs.com/findbetterme/p/10787118.html 看这个就完事了 1. 统计两个数的二进制表示有多少位不同 461. Hamming Dista ...

  9. Leedcode算法专题训练(数组与矩阵)

    1. 把数组中的 0 移到末尾 283. Move Zeroes (Easy) Leetcode / 力扣 class Solution { public void moveZeroes(int[] ...

随机推荐

  1. 为什么说NGK公链的商用落地是可行的?

    互联网.大数据以及云计算的发展给人们的生活.工作带来了诸多便利,也让人们一次又一次感叹科技的进步.而NGK公链的诞生,更是让众人称之为传奇.其商用落地可行性,也让人惊叹.那么,为什么说NGK公链的商用 ...

  2. 精密进近OCH的计算

    一.计算步骤 以I类精密进近为例,运行标准的制定大致分为以下几个步骤: 1)确定精密航段的超高障碍物. 2)计算当量高 3)计算高度损失 4)当量高与高度损失相加得到超障高OCH 5)对复飞段障碍物进 ...

  3. 基于西门子S7-1500的大型焊接机全套程序,使用博图V14打开(带全部注释)

    程序说明:本套程序是在从事自动化行业时候的做的项目的程序,经过在设备上运行测试,其中包含20多个轴的伺服控制以及模拟量,数字量IO的控制,包括扫描枪的读取,属于大型程序,总步数有好几万步. 本程序注释 ...

  4. WPF -- 构建动画

    写在前面:本文代码摘自<Head First C#> 本文使用ObjectAnimationUsingKeyFrames + Storyboard构建一个动画. ObjectAnimati ...

  5. Android7.0无需FileProvide搞定URI拍照、应用安装问题

    根据官方文档,从Android7.0版本开始 使用URI打开或安装文件需要单独在应用里配置了,问了度娘,有好多版本的结果,个人认为最靠谱的就是下边这个方法,只需在application的oncreat ...

  6. css3自动换行排列

    如果一行放不下就会自动换行 display: flex; flex-wrap: wrap; 示例 : html <div class="container"> < ...

  7. arti车是大幅度发地方大幅度发

    转: arti车是大幅度发地方大幅度发 arti车是大幅度发地方大幅度发 转: arti车是大幅度发地方大幅度发

  8. [译]我是如何将GTA在线模式的加载时间缩短70%的

    [译]我是如何将GTA在线模式的加载时间缩短70%的 译注: 最近在网上发现了一篇有意思的文章, 一个国外大神受不了GTA5在线模式的加载时间, 一怒之下反汇编了GTA5的源码, 并最终发现了问题的原 ...

  9. 致被职场PUA的打工人

    作为打工人,除了每天面对着各种繁琐的工作,还要被动接受上级或多或少的PUA,实在是难上加难,甚至有人想不开而自杀.网络上最近流行了一个词:职场PUA,赋予了这种现象一个正式的名字. 职场PUA指的是职 ...

  10. 24端口以太网FPGA的开发板

    板卡架构 板载FPGA(K7-325T)处理24端口10/100/1000M以太网数据: FPGA外挂4Gbit的DDR3颗粒,最大支持800MHz: 板载CPU进行系统配置.管理,并与客户端软件通信 ...