739. 每日温度
题目链接:https://leetcode.cn/problems/daily-temperatures/
文档讲解:https://programmercarl.com/0739.%E6%AF%8F%E6%97%A5%E6%B8%A9%E5%BA%A6.html
视频讲解:https://www.bilibili.com/video/BV1my4y1Z7jj/
思路
为什么考虑使用单调栈:通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置,此时我们就要想到可以用单调栈了。时间复杂度为O(n)。找右边比自己大的元素,需要保持单调栈单调递增。
单调栈的作用:用一个栈来记录我们遍历过的元素,因为我们遍历数组的时候,我们不知道之前都遍历了哪些元素,以至于遍历一个元素找不到是不是之前遍历过一个更小的,所以我们需要用一个容器(这里用单调栈)来记录我们遍历过的元素。
单调栈里存放的元素是什么:单调栈里只需要存放元素的下标i就可以了,如果需要使用对应的元素,直接T[i]就可以获取。
使用单调栈主要有三个判断条件。
- 当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况:将T[i]压入栈。
- 前遍历的元素T[i]等于栈顶元素T[st.top()]的情况:将T[i]压入栈。
- 当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况:计算结果到res中。
代码
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
Deque<Integer> st = new LinkedList<>();
int[] res = new int[temperatures.length];
st.push(0);
for (int i = 1; i < temperatures.length; i++) {
while (!st.isEmpty() && temperatures[i] > temperatures[st.peek()]) {
res[st.peek()] = i - st.peek();
st.pop();
}
st.push(i);
}
return res;
}
}
分析:时间复杂度:O(n),空间复杂度:O(n)。
496.下一个更大元素 I
题目链接:https://leetcode.cn/problems/next-greater-element-i/
文档讲解:https://programmercarl.com/0496.%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83…
视频讲解:https://www.bilibili.com/video/BV1jA411m7dX/
思路
使用map来存储nums1中元素和下标的对应关系,用单调栈遍历nums2,如果2中的元素在1中,存入1中下标对应的res数组。
代码
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Deque<Integer> st = new LinkedList<>();
int[] res = new int[nums1.length];
Arrays.fill(res, -1);
HashMap<Integer, Integer> map = new HashMap<>();
st.push(0);
for (int i = 0; i < nums1.length; i++) map.put(nums1[i], i);
for (int i = 1; i < nums2.length; i++) {
while (!st.isEmpty() && nums2[i] > nums2[st.peek()]) {
if (map.containsKey(nums2[st.peek()])) {
int index = map.get(nums2[st.peek()]);
res[index] = nums2[i];
}
st.pop();
}
st.push(i);
}
return res;
}
}
分析:时间复杂度:O(m + n),空间复杂度:O(m + n)。m是nums1.length,n是nums2.length。
503.下一个更大元素II
题目链接:https://leetcode.cn/problems/next-greater-element-ii/
文档讲解:https://programmercarl.com/0503.%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83%E7…
视频讲解:https://www.bilibili.com/video/BV15y4y1o7Dw/
思路
我的思路:遍历一次数组后,栈当中剩下的元素要么是最大值,要么是比他大的值在他前面,所以再遍历一次数组,并使用最大值进行比较,如果栈中弹出的元素是最大值,就赋值为-1,否则为他找更大值。
卡哥思路:用i取模来模拟成环的遍历。
代码
我的思路
class Solution {
public int[] nextGreaterElements(int[] nums) {
Deque<Integer> stack = new LinkedList<>();
int len = nums.length;
int[] res = new int[len];
stack.push(0);
int max = nums[0];
for (int i = 1; i < len; i++) {
max = Math.max(max, nums[i]);
while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) {
res[stack.peek()] = nums[i];
stack.pop();
}
stack.push(i);
}
for (int i = 0; i < len; i++) {
if (!stack.isEmpty() && max == nums[stack.peek()]) {
res[stack.peek()] = -1;
stack.pop();
}
while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) {
res[stack.peek()] = nums[i];
stack.pop();
}
if (stack.isEmpty()) break;
}
return res;
}
}
卡哥思路
class Solution {
public int[] nextGreaterElements(int[] nums) {
Deque<Integer> stack = new LinkedList<>();
int len = nums.length;
int[] res = new int[len];
Arrays.fill(res, -1);
stack.push(0);
for (int i = 1; i < len * 2; i++) {
while (!stack.isEmpty() && nums[i % len] > nums[stack.peek()]) {
res[stack.peek()] = nums[i % len];
stack.pop();
}
stack.push(i % len);
}
return res;
}
}
分析:时间复杂度:O(n),空间复杂度:O(n)。
复习字符串
151.翻转字符串里的单词
卡码网:55.右旋转字符串
28. 实现 strStr()
459.重复的子字符串
字符串总结
418

被折叠的 条评论
为什么被折叠?



