当前位置:网站首页>LeetCode 503. 下一个更大元素 II
LeetCode 503. 下一个更大元素 II
2022-07-05 09:16:00 【Sasakihaise_】

【单调栈】把最后一个元素之前的那些元素先入栈,形成环,再按照单调栈的方法遍历即可:
即对于每个元素i,把栈中比他小的元素全部出栈,此时如果栈顶还有,那么栈顶就是第一个比他大的,如果为空说明没有。最后不要忘了把元素i入栈。
class Solution {
// 单调栈 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;
}
}
边栏推荐
- L'information et l'entropie, tout ce que vous voulez savoir est ici.
- Transfer learning and domain adaptation
- Wxss template syntax
- Progressive JPEG pictures and related
- Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
- 【ManageEngine】如何利用好OpManager的报表功能
- Kotlin introductory notes (VI) interface and function visibility modifiers
- OpenGL - Lighting
- Mengxin summary of LCs (longest identical subsequence) topics
- Understanding rotation matrix R from the perspective of base transformation
猜你喜欢
![[code practice] [stereo matching series] Classic ad census: (5) scan line optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization

Applet data attribute method

Add discount recharge and discount shadow ticket plug-ins to the resource realization applet

Introduction Guide to stereo vision (7): stereo matching

Introduction Guide to stereo vision (1): coordinate system and camera parameters

OpenGL - Coordinate Systems

优先级队列(堆)

顶会论文看图对比学习(GNN+CL)研究趋势
![Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]](/img/ed/0483c529db2af5b16b18e43713d1d8.jpg)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]

Blogger article navigation (classified, real-time update, permanent top)
随机推荐
优先级队列(堆)
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
什么是防火墙?防火墙基础知识讲解
驾驶证体检医院(114---2 挂对应的医院司机体检)
Confusion matrix
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
Composition of applet code
My experience from technology to product manager
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
2310. 个位数字为 K 的整数之和
STM32简易多级菜单(数组查表法)
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
一篇文章带你走进cookie,session,Token的世界
Use arm neon operation to improve memory copy speed
Applet (subcontracting)
高性能Spark_transformation性能
Kotlin introductory notes (VII) data class and singleton class
C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边
交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒