当前位置:网站首页>LeetCode 503. Next bigger Element II
LeetCode 503. Next bigger Element II
2022-07-05 09:26:00 【Sasakihaise_】
【 Monotonic stack 】 Put those elements before the last element on the stack first , Form a ring , Then follow the monotonic stack method to traverse :
That is, for each element i, Put all the elements smaller than him out of the stack , At this time, if there is still , Then the top of the stack is the first one bigger than him , If it is empty, it means there is no . Finally, don't forget to put the elements i Push .
class Solution {
// Monotonic stack 3:15 3:24
public int[] nextGreaterElements(int[] nums) {
Deque<Integer> stack = new LinkedList();
int i, j, n = nums.length;
for (i = n - 2; i >= 0; i--) {
while (!stack.isEmpty() && stack.peek() <= nums[i]) {
stack.pop();
}
stack.push(nums[i]);
}
int[] ans = new int[n];
for (i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && stack.peek() <= nums[i]) {
stack.pop();
}
if (stack.isEmpty()) {
ans[i] = -1;
} else {
ans[i] = stack.peek();
}
stack.push(nums[i]);
}
return ans;
}
}
边栏推荐
- Kotlin introductory notes (II) a brief introduction to kotlin functions
- L'information et l'entropie, tout ce que vous voulez savoir est ici.
- 一篇文章带你走进cookie,session,Token的世界
- Luo Gu p3177 tree coloring [deeply understand the cycle sequence of knapsack on tree]
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- Applet (subcontracting)
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
- STM32简易多级菜单(数组查表法)
- Causes and appropriate analysis of possible errors in seq2seq code of "hands on learning in depth"
- Nodejs modularization
猜你喜欢
什么是防火墙?防火墙基础知识讲解
Progressive JPEG pictures and related
Global configuration tabbar
Hosting environment API
Rebuild my 3D world [open source] [serialization-1]
Information and entropy, all you want to know is here
混淆矩阵(Confusion Matrix)
编辑器-vi、vim的使用
Principle and performance analysis of lepton lossless compression
Node collaboration and publishing
随机推荐
Transfer learning and domain adaptation
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
深入浅出PyTorch中的nn.CrossEntropyLoss
3D reconstruction open source code summary [keep updated]
An article takes you into the world of cookies, sessions, and tokens
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
混淆矩阵(Confusion Matrix)
Nodejs modularization
迁移学习和域自适应
Nodemon installation and use
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
C # draw Bezier curve with control points for lattice images and vector graphics
Summary of "reversal" problem in challenge Programming Competition
Wxss template syntax
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
2309. 兼具大小写的最好英文字母
[beauty of algebra] solution method of linear equations ax=0
Applet (use of NPM package)
Kotlin introductory notes (V) classes and objects, inheritance, constructors