代码随想录Day 50|Leetcode|Python|739. 每日温度,496. 下一个更大元素 I

739. 每日温度

给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。

解题思路:

本题要使用单调栈,对于单调递增或递减,不论哪个方向,使用单调栈会更方便解题,单调栈用来存储index,步骤如下:

  1. 建立一个和temperature等长的result list存储index,建立stack,先将index 0 append in
  2. 建立for loop遍历剩下温度,和stack[-1]进行对比,分类讨论
    1. current temperatures[i] <= stack[-1]: result.append(idx)
    2. 建立while loop,循环条件为stack exists and current > stack[-1],result[stack[-1]] = append(i-stack[-1]), stack.pop()
  3. 初始化result所有值为0
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        result = [0]*len(temperatures)
        stack = []
        stack.append(0)
        for i in range(1, len(temperatures)):
            print(i)
            if temperatures[i] <= temperatures[stack[-1]]:
                stack.append(i)
            else:
                while stack and temperatures[i]>temperatures[stack[-1]]:
                    # print(stack[-1])
                    result[stack[-1]] = i - stack[-1]
                    stack.pop()
                stack.append(i)
        return result

496. 下一个更大元素 I

nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。

给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。

对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。

返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。

解题思路:

本题与上一题类似,区别在于建立nums1和nums2映射关系,nums1中的数字应该在nums2中得到相应下标idx,找到后步骤如下:

  1. 建立一个和nums1等长的result list存储的 index,建立stack用来遍历nums2,先将index 0 append in
  2. 建立for loop遍历nums2,和stack[-1]进行对比,建立while loop,循环条件为stack exists and nums2[i]> nums2[stack[-1]],判断当前nums2[i]是否在nums1中,result[idx] = nums2[i], stack.pop(),加上当前值进行下一轮判断
  3. 初始化result所有值为-1
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        result = [-1]*len(nums1)
        stack = []
        for i in range(len(nums2)):
            while stack and nums2[i]>nums2[stack[-1]]:
                if nums2[stack[-1]] in nums1:
                    idx = nums1.index(nums2[stack[-1]])
                    result[idx] = nums2[i]
                stack.pop()
            stack.append(i)
        return result

方法二:

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        result = [-1]*len(nums1)
        for i in range(len(nums1)):
            idx = nums2.index(nums1[i])#get the index of nums1 in nums2
            
            for j in range(idx+1, len(nums2)):
                if nums2[j] > nums2[idx]:
                    result[i] = nums2[j]
                    break
        return result
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值