力扣(2024.06.19)

1. 42------接雨水

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

**标签:**数组,双指针

代码:

python 复制代码
class Solution:
    def trap(self, height: List[int]) -> int:
        max_left = 0
        max_right = 0
        left = []
        right = []
        res = 0
        for i in range(0, len(height) - 2, 1):
            if height[i] > max_left:
                max_left = height[i]
            left.append(max_left)
        for i in range(len(height) - 1, 1, -1):
            if height[i] > max_right:
                max_right = height[i]
            right.append(max_right)
        right = right[::-1]
        for i in range(1, len(height) - 1):
            if min(left[i-1], right[i-1]) > height[i]:
                res = res + min(left[i-1], right[i-1]) - height[i]
        return res

2. 43------字符串相乘

给定两个以字符串形式表示的非负整数 num1num2,返回 num1num2 的乘积,它们的乘积也表示为字符串形式。

注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。

**标签:**数学,字符串,模拟

代码:

python 复制代码
class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        sum = 0
        leng1 = len(num1)
        for i in num1:
            leng2 = len(num2)
            for j in num2:
                sum = sum + int(i) * 10 ** (leng1 - 1) * int(j) * 10 ** (leng2 - 1)
                leng2 = leng2 - 1
            leng1 = leng1 - 1
        return str(sum)

3. 44------通配符匹配

给你一个输入字符串 (s) 和一个字符模式 (p) ,请你实现一个支持 '?''*' 匹配规则的通配符匹配:

  • '?' 可以匹配任何单个字符。
  • '*' 可以匹配任意字符序列(包括空字符序列)。

判定匹配成功的充要条件是:字符模式必须能够完全匹配输入字符串(而不是部分匹配)。

**标签:**贪心,递归,字符串,动态规划(目前不会)

代码:

4. 45------跳跃游戏2

给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]

每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i + j] 处:

  • 0 <= j <= nums[i]
  • i + j < n

返回到达 nums[n - 1] 的最小跳跃次数。生成的测试用例可以到达 nums[n - 1]

**标签:**贪心,数组,动态规划(目前不会)

代码:

5. 46------全排列

给定一个不含重复数字的数组 nums ,返回其所有可能的全排列 。你可以按任意顺序返回答案。

**标签:**数组,回溯

代码:

python 复制代码
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        def backtrack(nums, used, res, path):
            if len(path) == len(nums):
                res.append(path.copy())
                return
            for i in range(len(nums)):
                if used[i]:
                    continue
                used[i] = 1
                path.append(nums[i])
                backtrack(nums, used, res, path)
                used[i] = 0
                path.pop()
        res = []
        path = []
        used = [0] * len(nums)
        backtrack(nums, used, res, path)
        return res

6. 47------全排列2

给定一个可包含重复数字的序列 nums ,按任意顺序返回所有不重复的全排列。

**标签:**数组,回溯

代码:

python 复制代码
class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        def backtrack(nums, used, res, path):
            if len(path) == len(nums):
                res.append(path.copy())
                return
            for i in range(len(nums)):
                if used[i]:
                    continue
                if i > 0 and nums[i] == nums[i - 1] and not used[i - 1]:
                    continue
                used[i] = 1
                path.append(nums[i])
                backtrack(nums, used, res, path)
                used[i] = 0
                path.pop()
        res = []
        path = []
        used = [0] * len(nums)
        nums.sort()
        backtrack(nums, used, res, path)
        return res
相关推荐
凌肖战9 分钟前
力扣上刷题之C语言实现(数组)
c语言·算法·leetcode
秋夫人36 分钟前
B+树(B+TREE)索引
数据结构·算法
鸽芷咕1 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
子午1 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期1 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
Milo_K1 小时前
今日 leetCode 15.三数之和
算法·leetcode
Darling_001 小时前
LeetCode_sql_day28(1767.寻找没有被执行的任务对)
sql·算法·leetcode
AlexMercer10121 小时前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法