leetcode 31-40(2024.08.18)

立个flag,1-100题每天分配10题,不会就先空着(2,8)。

昨天周六,摆烂一天。

  1. 31:下一个排列
python 复制代码
class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        i = len(nums) - 1
        while i > 0:
            if nums[i - 1] < nums[i]:
                break
            else:
                i = i - 1
        if i == 0:
            nums.sort()
        else:
            idx1 = i - 1
            num1 = nums[idx1]
            i = len(nums) - 1
            while i > idx1:
                if nums[i] > num1:
                    break
                else:
                    i = i - 1
            idx2 = i
            num2 = nums[idx2]
            nums[idx1], nums[idx2] = num2, num1
            left = idx1 + 1
            right = len(nums) - 1
            while left < right:
                nums[left], nums[right] = nums[right], nums[left]
                left = left + 1
                right = right - 1
  1. 32:最长有效括号

  2. 33:搜索旋转排序数组

python 复制代码
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums) - 1
        while left <= right:
            mid = left + (right - left) // 2
            if nums[mid] == target:
                return mid
            if nums[left] <= nums[mid]:
                if nums[left] <= target < nums[mid]:
                    right = mid - 1
                else:
                    left = mid + 1
            elif nums[left] > nums[mid]:
                if nums[mid] < target <= nums[right]:
                    left = mid + 1
                else:
                    right = mid - 1
        return -1
  1. 34:在排序数组中查找元素的第一个和最后一个位置
python 复制代码
class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        def searchLeft(nums, target):
            left = 0
            right = len(nums) - 1
            while left <= right:
                mid = left + (right - left) // 2
                if nums[mid] < target:
                    left = mid + 1
                elif nums[mid] >= target:
                    right = mid - 1
            if nums[left] == target:
                return left
            else:
                return -1
        
        def searchRight(nums, target):
            left = 0
            right = len(nums) - 1
            while left <= right:
                mid = left + (right - left) // 2
                if nums[mid] > target:
                    right = mid - 1
                elif nums[mid] <= target:
                    left = mid + 1
            if nums[right] == target:
                return right
            else:
                return -1

        if len(nums) == 0 or nums[0] > target or nums[-1] < target:
            return [-1, -1]
        else:
            left = searchLeft(nums, target)
            right = searchRight(nums, target)
            return [left, right]
  1. 35:搜索插入位置
python 复制代码
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums) - 1
        while left <= right:
            mid = left + (right - left) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                left = mid + 1
            elif nums[mid] > target:
                right = mid - 1
        return left
  1. 36:有效的数独
python 复制代码
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        hang = []
        lie = []
        gong = []
        for i in range(9):
            hang.append([])
            lie.append([])
            gong.append([])
        for i in range(9):
            for j in range(9):
                num = board[i][j]
                if num == ".":
                    continue
                if num in hang[i] or num in lie[j] or num in gong[3 * (i // 3) + 1 * (j // 3)]:
                    return False
                hang[i].append(num)
                lie[j].append(num)
                gong[3 * (i // 3) + 1 * (j // 3)].append(num)
        return True
  1. 37:解数独
python 复制代码
class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        def isValid(board, i, j, n):
            for k in range(9):
                if n == board[k][j] or n == board[i][k]:
                    return False
            start_row = (i // 3) * 3
            start_col = (j // 3) * 3
            for i in range(start_row, start_row + 3):
                for j in range(start_col, start_col + 3):
                    if n == board[i][j]:
                        return False
            return True

        def backtracking(board):
            for i in range(9):
                for j in range(9):
                    if board[i][j] != '.':
                        continue
                    for num in range(1, 10):
                        if isValid(board, i, j, str(num)):
                            board[i][j] = str(num)
                            if backtracking(board):
                                return True
                            board[i][j] = '.'
                    return False
            return True

        backtracking(board)
  1. 38:外观数列

  2. 39:组合总和

python 复制代码
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        def backtracking(candidates, target, start, path, res):
            if sum(path) == target:
                res.append(path.copy())
                return
            for i in range(start, len(candidates)):
                if candidates[i] + sum(path) > target:
                    break
                path.append(candidates[i])
                backtracking(candidates, target, i, path, res)
                path.pop()

        res = []
        path = []
        start = 0
        candidates.sort()
        backtracking(candidates, target, start, path, res)
        return res
  1. 40:组合总和2
python 复制代码
class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        def backtracking(candidates, target, start, path, res):
            if sum(path) == target:
                res.append(path.copy())
                return
            for i in range(start, len(candidates)):
                if i > start and candidates[i] == candidates[i - 1]:
                    continue
                if candidates[i] + sum(path) > target:
                    break
                path.append(candidates[i])
                backtracking(candidates, target, i + 1, path, res)
                path.pop()

        res = []
        path = []
        start = 0
        candidates.sort()
        backtracking(candidates, target, start, path, res)
        return res
相关推荐
木向23 分钟前
leetcode22:括号问题
开发语言·c++·leetcode
comli_cn24 分钟前
使用清华源安装python包
开发语言·python
赵谨言34 分钟前
基于python 微信小程序的医院就诊小程序
经验分享·python·毕业设计
蹉跎x36 分钟前
力扣1358. 包含所有三种字符的子字符串数目
数据结构·算法·leetcode·职场和发展
1.01^10001 小时前
[1111].集成开发工具Pycharm安装与使用
python·pycharm
HEX9CF1 小时前
【Django】测试带有 CSRF 验证的 POST 表单 API 报错:Forbidden (CSRF cookie not set.)
python·django·csrf
坊钰1 小时前
【Java 数据结构】移除链表元素
java·开发语言·数据结构·学习·链表
凡人的AI工具箱2 小时前
每天40分玩转Django:实操多语言博客
人工智能·后端·python·django·sqlite
Py办公羊大侠2 小时前
Excel批量设置行高,Excel表格设置自动换行后打印显示不全,Excel表格设置最合适的行高后打印显示不全,完美解决方案!!!
python·excel·打印·openpyxl·自动换行·显示不全
PieroPc2 小时前
Python tkinter写的《电脑装配单》和 Html版 可打印 可导出 excel 文件
python·html·电脑