当前位置:网站首页>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;
}
}
边栏推荐
- 牛顿迭代法(解非线性方程)
- . Net service governance flow limiting middleware -fireflysoft RateLimit
- Codeworks round 681 (Div. 2) supplement
- Wechat applet obtains household area information
- C # image difference comparison: image subtraction (pointer method, high speed)
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- 混淆矩阵(Confusion Matrix)
- Luo Gu p3177 tree coloring [deeply understand the cycle sequence of knapsack on tree]
- STM32 simple multi-level menu (array table lookup method)
- [reading notes] Figure comparative learning gnn+cl
猜你喜欢
Applet (subcontracting)
Composition of applet code
Applet global style configuration window
Progressive JPEG pictures and related
Shutter uses overlay to realize global pop-up
C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边
OpenGL - Coordinate Systems
22-07-04 西安 尚好房-项目经验总结(01)
c语言指针深入理解
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
随机推荐
[reading notes] Figure comparative learning gnn+cl
What is a firewall? Explanation of basic knowledge of firewall
Blogger article navigation (classified, real-time update, permanent top)
驾驶证体检医院(114---2 挂对应的医院司机体检)
uni-app 实现全局变量
OpenGL - Coordinate Systems
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
Wxml template syntax
nodejs_ fs. writeFile
Creation and reference of applet
项目实战 | Excel导出功能
Information and entropy, all you want to know is here
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
scipy. misc. imread()
一次 Keepalived 高可用的事故,让我重学了一遍它
22-07-04 西安 尚好房-项目经验总结(01)
Applet (global data sharing)
LeetCode 496. 下一个更大元素 I
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势