当前位置:网站首页>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;
}
}
边栏推荐
- Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding
- . Net service governance flow limiting middleware -fireflysoft RateLimit
- OpenGL - Coordinate Systems
- Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
- Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
- 交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
- Summary of "reversal" problem in challenge Programming Competition
- nodejs_ 01_ fs. readFile
- C#图像差异对比:图像相减(指针法、高速)
- 利用请求头开发多端应用
猜你喜欢
![Rebuild my 3D world [open source] [serialization-1]](/img/74/b6253845b43bc18f425d57695fba7c.jpg)
Rebuild my 3D world [open source] [serialization-1]

Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning

Kotlin introductory notes (VIII) collection and traversal

Applet customization component

C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边

Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding

Priority queue (heap)
![C [essential skills] use of configurationmanager class (use of file app.config)](/img/8b/e56f87c2d0fbbb1251ec01b99204a1.png)
C [essential skills] use of configurationmanager class (use of file app.config)
![Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]](/img/7d/e7370e757c18b3dbb47e633703c346.jpg)
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
![[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
随机推荐
Applet data attribute method
My life
Editor use of VI and VIM
Applet customization component
2311. 小于等于 K 的最长二进制子序列
Rebuild my 3D world [open source] [serialization-2]
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
Priority queue (heap)
Nodejs modularization
Codeworks round 638 (Div. 2) cute new problem solution
云计算技术热点
Transfer learning and domain adaptation
Hosting environment API
Kotlin introductory notes (I) kotlin variables and non variables
我的一生.
c#比较两张图像的差异
基于STM32单片机的测温仪(带人脸检测)
驾驶证体检医院(114---2 挂对应的医院司机体检)
阿里十年测试带你走进APP测试的世界
Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"