当前位置:网站首页>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;
}
}
边栏推荐
- 22-07-04 西安 尚好房-项目经验总结(01)
- OpenGL - Model Loading
- Understanding rotation matrix R from the perspective of base transformation
- Lepton 无损压缩原理及性能分析
- Newton iterative method (solving nonlinear equations)
- fs. Path module
- Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
- C form click event did not respond
- [technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
- Composition of applet code
猜你喜欢
C form click event did not respond
Kotlin introductory notes (II) a brief introduction to kotlin functions
Rebuild my 3D world [open source] [serialization-1]
Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
Applet (global data sharing)
nodejs_ 01_ fs. readFile
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
OpenGL - Model Loading
利用请求头开发多端应用
Huber Loss
随机推荐
顶会论文看图对比学习(GNN+CL)研究趋势
Figure neural network + comparative learning, where to go next?
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
利用请求头开发多端应用
迁移学习和域自适应
Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
STM32 simple multi-level menu (array table lookup method)
OpenGL - Coordinate Systems
scipy.misc.imread()
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
Jenkins pipeline method (function) definition and call
Greendao reported an error in qigsaw, could not init daoconfig
C # draw Bezier curve with control points for lattice images and vector graphics
nodejs_ 01_ fs. readFile
[beauty of algebra] solution method of linear equations ax=0
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
Introduction Guide to stereo vision (7): stereo matching
Applet customization component
What is a firewall? Explanation of basic knowledge of firewall