739. 每日温度
- 刷题
https://leetcode.cn/problems/daily-temperatures/description/
- 文章讲解
https://programmercarl.com/0739.%E6%AF%8F%E6%97%A5%E6%B8%A9%E5%BA%A6.html
- 视频讲解
https://www.bilibili.com/video/BV1my4y1Z7jj/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解:
public int[] dailyTemperatures(int[] T) {
int length = T.length;
int[] result = new int[length];
for (int i = 0; i < length; i++) {
int current = T[i];
if (current < 100) {
for (int j = i + 1; j < length; j++) {
if (T[j] > current) {
result[i] = j - i;
break;
}
}
}
}
return result;
}
496.下一个更大元素 I
- 刷题
https://leetcode.cn/problems/next-greater-element-i/description/
- 文章讲解
https://programmercarl.com/0496.%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83%E7%B4%A0I.html
- 视频讲解
https://www.bilibili.com/video/BV1jA411m7dX/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解:
public class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
Deque<Integer> stack = new ArrayDeque<>();
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < len2; i++) {
while (!stack.isEmpty() && stack.peekLast() < nums2[i]) {
map.put(stack.removeLast(), nums2[i]);
}
stack.addLast(nums2[i]);
}
int[] ans = new int[len1];
for (int i = 0; i < len1; i++) {
ans[i] = map.getOrDefault(nums1[i], -1);
}
return ans;
}
}