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
相关推荐
麦麦大数据1 分钟前
Python棉花病虫害图谱系统CNN识别+AI问答知识neo4j vue+flask深度学习神经网络可视化
人工智能·python·深度学习
LKID体4 分钟前
Python操作neo4j库py2neo使用之创建和查询(二)
数据库·python·neo4j
LKID体8 分钟前
Python操作neo4j库py2neo使用之py2neo 删除及事务相关操作(三)
开发语言·python·neo4j
小屁孩大帅-杨一凡9 分钟前
Python-flet实现个人视频播放器
开发语言·python·音视频
算家云12 分钟前
快速识别模型:simple_ocr,部署教程
开发语言·人工智能·python·ocr·数字识别·检测模型·英文符号识别
Thomas_Cai23 分钟前
Python后端flask框架接收zip压缩包方法
开发语言·python·flask
亚图跨际1 小时前
Python和R荧光分光光度法
开发语言·python·r语言·荧光分光光度法
Hera_Yc.H1 小时前
数据结构之一:复杂度
数据结构
谢眠1 小时前
深度学习day3-自动微分
python·深度学习·机器学习