leetcode 47. 全排列 II 48. 组合总和 II

leetcode 47. 全排列 II 48. 组合总和 II

47. 全排列 II

难度中等865收藏分享切换为英文接收动态反馈

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

示例 1:

输入:nums = [1,1,2]
输出:
[[1,1,2],
 [1,2,1],
 [2,1,1]]

示例 2:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

提示:

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10

通过次数231,822

提交次数362,400

# leetcode submit region begin(Prohibit modification and deletion)
from typing import List


class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        def dfs(nums: List[int]):
            arr = []
            if len(nums) == 2:
                if nums[0] != nums[1]:
                    return [[nums[0], nums[1]], [nums[1], nums[0]]]
                return [nums]
            keys = []
            for i in range(len(nums)):
                if nums[i] in keys:
                    continue
                nums2 = nums.copy()
                keys.append(nums[i])
                del nums2[i]
                r = dfs(nums2)
                for k in r:
                    k.insert(0, nums[i])
                    arr.append(k)
            return arr
        if len(nums) < 2:
            return [nums]
        return dfs(nums)

    def permuteUnique2(self, nums: List[int]) -> List[List[int]]:
        def dfs(nums: List[int]):
            arr = []
            if len(nums) == 2:
                if nums[0] != nums[1]:
                    return [[nums[0], nums[1]], [nums[1], nums[0]]]
                return [nums]
            for i in range(len(nums)):
                nums2 = nums.copy()
                del nums2[i]
                r = dfs(nums2)
                for k in r:
                    k.insert(0, nums[i])
                    if k not in arr:
                        arr.append(k)
            return arr
        if len(nums) < 2:
            return [nums]
        return dfs(nums)
# leetcode submit region end(Prohibit modification and deletion)
if __name__ == '__main__':
    nums = [1, 1, 2]
    # nums = [1, 1]
    print(Solution().permuteUnique(nums))

48. 旋转图像

难度中等1059收藏分享切换为英文接收动态反馈

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在** 原地** 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例 1:

leetcode 47. 全排列 II 48. 组合总和 II

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]

示例 2:

leetcode 47. 全排列 II 48. 组合总和 II

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

示例 3:

输入:matrix = [[1]]
输出:[[1]]

示例 4:

输入:matrix = [[1,2],[3,4]]
输出:[[3,1],[4,2]]

提示:

  • matrix.length == n
  • matrix[i].length == n
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000
# leetcode submit region begin(Prohibit modification and deletion)
from typing import List


class Solution:

    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        length = len(matrix) - 1
        for i in range((length + 1) // 2):
            for j in range(i,length - i):
                # tmp = matrix[i][j]
                matrix[i][j], matrix[length - j][i], matrix[length - i][length - j], matrix[j][length - i] = \
                    matrix[length - j][i], matrix[length - i][length - j], matrix[j][length - i], matrix[i][j]
                # matrix[i][j] = matrix[length - j][i]
                # matrix[length - j][i] = matrix[length - i][length - j]
                # matrix[length - i][length - j] = matrix[j][length - i]
                # matrix[j][length - i] = tmp
        return matrix

    def rotate2(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        length = len(matrix)
        a = [[0 for _ in range(length)] for _ in range(length)]
        for j in range(length):
            for k in range(length):
                a[k][length - 1 - j] = matrix[j][k]
        matrix[:] = a

    def rotate3(self, matrix: List[List[int]]) -> None:
        n = len(matrix)
        # 水平翻转
        for i in range(n // 2):
            for j in range(n):
                matrix[i][j], matrix[n - i - 1][j] = matrix[n - i - 1][j], matrix[i][j]
        # 主对角线翻转
        for i in range(n):
            for j in range(i):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

# leetcode submit region end(Prohibit modification and deletion)
if __name__ == '__main__':
    matrix = [[1,2,3],[4,5,6],[7,8,9]]
    matrix = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]]
    print(Solution().rotate(matrix))
deletion)
if __name__ == '__main__':
    matrix = [[1,2,3],[4,5,6],[7,8,9]]
    matrix = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]]
    print(Solution().rotate(matrix))
上一篇:48-权限控制


下一篇:每日总结48