截止到目前我已经写了 500多道算法题,其中部分已经整理成了pdf文档,目前总共有1000多页(并且还会不断的增加),大家可以免费下载

LeetCode 496. 下一个更大元素 I_数组
LeetCode 496. 下一个更大元素 I_单调栈_02
​​​视频链接​

public int[] nextGreaterElement(int[] nums1, int[] nums2) {
//map中的key是数组中元素的值,value是这个值遇到的
//右边第一个比他大的值
Map<Integer, Integer> map = new HashMap<>();
//单调栈,从栈顶到栈底是递增的
Stack<Integer> stack = new Stack<>();
//遍历nums2的所有元素
for (int num : nums2) {
//如果栈顶元素小于num,说明栈顶元素遇到了右边
//第一个比他大的值,然后栈顶元素出栈,记录下
//这个值。
while (!stack.empty() && stack.peek() < num)
map.put(stack.pop(), num);
//当前元素入栈
stack.push(num);
}
//遍历nums1的所有元素,然后在map中查找,如果没有查找到,
//说明没有遇到右边比他大的值,默认给-1。
int[] res = new int[nums1.length];
for (int i = 0; i < nums1.length; i++) {
res[i] = map.getOrDefault(nums1[i], -1);
}
return res;
}

LeetCode 496. 下一个更大元素 I_LeetCode_03