1. 全排列 II
题目链接:https://leetcode.cn/problems/permutations-ii/
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
class Solution:def permuteUnique(self, nums: List[int]) -> List[List[int]]:def backtrack(nums, path, res, used):if len(path) == len(nums):res.append(path)returnfor i in range(len(nums)):if used[i] or (i > 0 and nums[i] == nums[i - 1] and not used[i - 1]):continueused[i] = Truebacktrack(nums, path + [nums[i]], res, used)used[i] = Falsenums.sort()res = []used = [False] * len(nums)backtrack(nums, [], res, used)return res
2. 旋转图像
题目链接:https://leetcode.cn/problems/rotate-image/
给定一个 n × n 的二维矩阵 matrix 表示一个图像。请将图像顺时针旋转 90 度。
class Solution:def rotate(self, matrix: List[List[int]]) -> None:"""Do not return anything, modify matrix in-place instead."""n = len(matrix)for i in range(n // 2):for j in range(i, n - i - 1):temp = matrix[i][j]matrix[i][j] = matrix[n - 1 - j][i]matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]matrix[j][n - 1 - i] = temp