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
相关推荐
Open-AI13 分钟前
Python如何判断一个数是几位数
python
极客代码16 分钟前
【Python TensorFlow】入门到精通
开发语言·人工智能·python·深度学习·tensorflow
义小深18 分钟前
TensorFlow|咖啡豆识别
人工智能·python·tensorflow
疯一样的码农23 分钟前
Python 正则表达式(RegEx)
开发语言·python·正则表达式
passer__jw76723 分钟前
【LeetCode】【算法】283. 移动零
数据结构·算法·leetcode
Ocean☾30 分钟前
前端基础-html-注册界面
前端·算法·html
顶呱呱程序38 分钟前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
爱吃生蚝的于勒1 小时前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
羊小猪~~1 小时前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
进击的六角龙1 小时前
Python中处理Excel的基本概念(如工作簿、工作表等)
开发语言·python·excel