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

【单调栈】从后往前遍历,对于元素i,不断弹出比i小的元素,如果栈不为空,那么栈顶就是第一个比i大的元素,然后把i押入栈。
class Solution {
// 单调栈找右侧第一个大于他的元素
// 1:55 2:14
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Deque<Integer> stack = new LinkedList();
Map<Integer, Integer> map = new HashMap();
for (var i = 0; i < nums2.length; i++) {
map.put(nums2[i], i);
}
int[] arr = new int[nums2.length];
for (var i = nums2.length - 1; i >= 0; i--) {
while (!stack.isEmpty()) {
int top = stack.peek();
if (top > nums2[i]) {
arr[i] = top;
stack.push(nums2[i]);
break;
} else {
stack.poll();
}
}
if (stack.isEmpty()) {
arr[i] = -1;
stack.push(nums2[i]);
}
}
int[] ans = new int[nums1.length];
for (var i = 0; i < nums1.length; i++) {
ans[i] = arr[map.get(nums1[i])];
}
return ans;
}
}
边栏推荐
- notepad++
- Huber Loss
- 混淆矩阵(Confusion Matrix)
- Attention is all you need
- Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
- [ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)
- 信息與熵,你想知道的都在這裏了
- Alibaba's ten-year test brings you into the world of APP testing
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- My life
猜你喜欢

高性能Spark_transformation性能

【阅读笔记】图对比学习 GNN+CL

优先级队列(堆)
![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]

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

NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA

嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!

Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)

RT thread kernel quick start, kernel implementation and application development learning with notes

Svg optimization by svgo
随机推荐
太不好用了,长文章加图文,今后只写小短文
Confusion matrix
Composition of applet code
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
顶会论文看图对比学习(GNN+CL)研究趋势
Mengxin summary of LCs (longest identical subsequence) topics
My experience from technology to product manager
C [essential skills] use of configurationmanager class (use of file app.config)
Jenkins pipeline method (function) definition and call
C # draw Bezier curve with control points for lattice images and vector graphics
阿里十年测试带你走进APP测试的世界
Understanding rotation matrix R from the perspective of base transformation
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
Applet global style configuration window
AdaBoost use
Golang foundation -- map, array and slice store different types of data
Editor use of VI and VIM
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)