代码随想录算法训练营第五十三天 | 739. 每日温度、496.下一个更大元素 I、503.下一个更大元素II、复习

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.重复的子字符串
字符串总结

  • 34
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第二十二算法训练营主要涵盖了Leetcode题目中的三道题目,分别是Leetcode 28 "Find the Index of the First Occurrence in a String",Leetcode 977 "有序数组的平方",和Leetcode 209 "长度最小的子数组"。 首先是Leetcode 28题,题目要求在给定的字符串中找到第一个出现的字符的索引。思路是使用双指针来遍历字符串,一个指向字符串的开头,另一个指向字符串的结尾。通过比较两个指针所指向的字符是否相等来判断是否找到了第一个出现的字符。具体实现的代码如下: ```python def findIndex(self, s: str) -> int: left = 0 right = len(s) - 1 while left <= right: if s[left == s[right]: return left left += 1 right -= 1 return -1 ``` 接下来是Leetcode 977题,题目要求对给定的有序数组中的元素进行平方,并按照非递减的顺序返回结果。这里由于数组已经是有序的,所以可以使用双指针的方法来解决问题。一个指针指向数组的开头,另一个指针指向数组的末尾。通过比较两个指针所指向的元素的绝对值的大小来确定哪个元素的平方应该放在结果数组的末尾。具体实现的代码如下: ```python def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 right = len(nums) - 1 ans = [] while left <= right: if abs(nums[left]) >= abs(nums[right]): ans.append(nums[left ** 2) left += 1 else: ans.append(nums[right ** 2) right -= 1 return ans[::-1] ``` 最后是Leetcode 209题,题目要求在给定的数组中找到长度最小的子数组,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值