leetcode列表--python

本文探讨多种算法挑战,包括预测未来气温、寻找数组中特定三元组、求最长公共前缀、最佳观光组合、文章断句以及股票交易策略。通过详细解析,展示算法设计与实现的精妙之处。

1.根据每日气温列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。(借用栈)

class Solution:
    def dailyTemperatures(self, T):
        ans = [0] * len(T)
        stack = []
        for i in range(len(T)):
            Tem = T[i]
            while stack and T[stack[-1]] < Tem:
                    prev_index = stack.pop()
                    ans[prev_index] = i - prev_index
            stack.append(i)
        return ans

2.给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ,找出所有满足条件且不重复的三元组。(排序后固定a,同时移动b,移动c)

class Solution:
    def threeSum(self, nums):
        n = len(nums)
        nums.sort()
        ans = list()
        # 枚举 a
        for first in range(n):
            # 需要和上一次枚举的数不相同
            if first > 0 and nums[first] == nums[first - 1]:
                continue
            # c 对应的指针初始指向数组的最右端
            third = n - 1
            target = -nums[first]
            # 枚举 b
            for second in range(first + 1, n):
                # 需要和上一次枚举的数不相同
                if second > first + 1 and nums[second] == nums[second - 1]:
                    continue
                # 需要保证 b 的指针在 c 的指针的左侧
                while second < third and nums[second] + nums[third] > target:
                    third -= 1
                # 如果指针重合,随着 b 后续的增加
                # 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
                if second == third:
                    break
                if nums[second] + nums[third] == target:
                    ans.append([nums[first], nums[second], nums[third]])

        return ans

3.编写一个函数来查找字符串数组中的最长公共前缀

class Solution:
    def longestCommonPrefix(self, strs):
        length = len(strs)
        if length == 0:
            return ''
        elif length == 1:
            return strs[0]
        pub = strs[0]
        for i in range(1, length):
            pub = self.str_common(pub, strs[i])
            if not pub:
                return ''
        return pub

    def str_common(self, prev, cur):
        length, index = min(len(prev), len(cur)), 0
        while index < length and prev[index] == cur[index]:
            index += 1
        return prev[:index]

4.最佳观光组合,给定正整数数组 A,A[i] 表示第 i 个观光景点的评分,并且两个景点 i 和 j 之间的距离为 j - i。一对景点(i < j)组成的观光组合的得分为(A[i] + A[j] + i - j):景点的评分之和减去它们两者之间的距离。返回一对观光景点能取得的最高分。

class Solution:
    def maxScoreSightseeingPair(self, A: List[int]) -> int:
        length = len(A)
        res = 0
        pre_max = A[0]
        for j in range(1, length):
            res = max(res, pre_max+A[j] - j)
            pre_max = max(pre_max, A[j] + j)
        return res

5.文章用 sentence 表示,设计一个算法,把文章断开,要求未识别的字符最少,返回未识别的字符数。

class Solution:
    def respace(self, dictionary: List[str], sentence: str) -> int:
        """
        动态规划,第 i 个字符无法与前面任何一个子串组成单词:f[i - 1] + 1
        		 第 i 个字符可以与前面某个子串组成单词:f[j: i],0 <= j <= i - 1
        """
        d = {}.fromkeys(dictionary)
        n = len(sentence)
        dp = [0] * (n + 1)
        for i in range(1, n + 1):
            f[i] = dp[i - 1] + 1
            for j in range(i):
                if sentence[j:i] in d:
                    dp[i] = min(dp[i], dp[j])
        return dp[-1]

6.(股票问题)给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。​计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):(1) 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。(2) 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        n = len(prices)
        dp = [0, 0, 0]
        dp[0] = -prices[0]
        for i in range(1, len(prices)):
            a = max(dp[0], dp[1]-prices[i])
            b = max(dp[2], dp[1])
            c = dp[0] + prices[i]
            dp[0], dp[1], dp[2] = a, b, c
        return max(dp[1], dp[2])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值