Leedcode算法专题训练(栈和队列)
1. 用栈实现队列
232. Implement Queue using Stacks (Easy)
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)
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)
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)
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)
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)
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算法专题训练(栈和队列)的更多相关文章
- Leedcode算法专题训练(搜索)
BFS 广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点.需要注意的是,遍历过的节点不能再次被遍历. 第一层: 0 -> {6,2,1,5} ...
- Leedcode算法专题训练(树)
递归 一棵树要么是空树,要么有两个指针,每个指针指向一棵树.树是一种递归结构,很多树的问题可以使用递归来处理. 1. 树的高度 104. Maximum Depth of Binary Tree (E ...
- Leedcode算法专题训练(贪心)
1. 分配饼干 455. 分发饼干 题目描述:每个孩子都有一个满足度 grid,每个饼干都有一个大小 size,只有饼干的大小大于等于一个孩子的满足度,该孩子才会获得满足.求解最多可以获得满足的孩子数 ...
- Leedcode算法专题训练(分治法)
归并排序就是一个用分治法的经典例子,这里我用它来举例描述一下上面的步骤: 1.归并排序首先把原问题拆分成2个规模更小的子问题. 2.递归地求解子问题,当子问题规模足够小时,可以一下子解决它.在这个例子 ...
- Leedcode算法专题训练(二分查找)
二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...
- Leedcode算法专题训练(排序)
排序 快速排序 用于求解 Kth Element 问题,也就是第 K 个元素的问题. 可以使用快速排序的 partition() 进行实现.需要先打乱数组,否则最坏情况下时间复杂度为 O(N2). 堆 ...
- Leedcode算法专题训练(双指针)
算法思想 双指针 167. 两数之和 II - 输入有序数组 双指针的典型用法 如果两个指针指向元素的和 sum == target,那么得到要求的结果: 如果 sum > target,移动较 ...
- Leedcode算法专题训练(位运算)
https://www.cnblogs.com/findbetterme/p/10787118.html 看这个就完事了 1. 统计两个数的二进制表示有多少位不同 461. Hamming Dista ...
- Leedcode算法专题训练(数组与矩阵)
1. 把数组中的 0 移到末尾 283. Move Zeroes (Easy) Leetcode / 力扣 class Solution { public void moveZeroes(int[] ...
随机推荐
- USDN代币的特点
USDN是NGK公链发行的算法型稳定币,采用智能合约发行,通过智能合约的透明化,能够让市场USND持有者获得算法稳定的背书.USDN是一种锚定全球通用的代币,更是连接全球数字经济的通用数字代币.USD ...
- 看超额担保免信任的NGK DeFi 乐高如何打造下一个千倍币?
2020年中,DeFi的高收益率吸引了大量热钱涌入,DeFi总锁仓量破百亿美金.如今,流动性挖矿的热潮暂时停歇,但对于 NGK DeFi项目来说,它背后的演变进化从未停止. 免信任是 NGK DeFi ...
- 人物传记STEPHEN LITAN:去中心化存储是Web3.0生态重要组成
近期,NGK.IO的开发团队首席技术官STEPHEN LITAN分享了自己对去中心化储存的观点,以下为分享内容. 目前的存储方式主要是集中式存储,随着数据规模和复杂度的迅速增加,集中存储的数据对于系统 ...
- 实现TensorRT-7.0插件自由!(如果不踩坑使用TensorRT插件功能)
本系列为新TensorRT的第一篇,为什么叫新,因为之前已经写了两篇关于TensorRT的文章,是关于TensorRT-5.0版本的.好久没写关于TensorRT的文章了,所幸就以新来开头吧~ 接下来 ...
- Nginx之Location匹配规则
概述 经过多年发展,nginx凭借其优异的性能征服了互联网界,成为了各个互联网公司架构设计中不可获取的要素.Nginx是一门大学问,但是对于Web开发者来说,最重要的是需要能捋的清楚Nginx的请求路 ...
- 渗透测试--Nmap主机识别
通过本篇博客可以学到:Nmap的安装和使用,列举远程机器服务端口,识别目标机器上的服务,指纹,发现局域网中存活主机,端口探测技巧,NSE脚本使用,使用特定网卡进行检测,对比扫描结果ndiff,可视化N ...
- springboot源码解析-管中窥豹系列之自动装配(九)
一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...
- 03----python入门----函数相关
一.前期知识储备 函数定义 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 () 任何传入参数和自变量必须放在圆括号中间,圆括号 ...
- FreeBSD 如何让csh像zsh那样具有命令错误修正呢
比如,,你用 emacs写c ,但你输完emacs ma按tab回车是,他会匹配所有ma开头的文件,而这个是忽略掉,也就是按tab时不会在有你忽略的东西,对编程之类的友好,不用再匹配到二进制..o之类 ...
- freebsd root 登录 KDE SDDM
sddm.conf 文件现在默认不会自动生成了.需要自己创建:ee /usr/local/etc/sddm.conf写入MinimumUid=0MaximumUid=00就是root用户.然后更改/u ...