Leetcode 47. 全排列 II (每日一题 20211015)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 47. 全排列 II (每日一题 20211015)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個可包含重復數字的序列 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]]鏈接:https://leetcode-cn.com/problems/permutations-iiclass Solution:def permuteUnique(self, nums: List[int]) -> List[List[int]]:nums.sort()res, path, used = [], [], [False] * len(nums)def dfs():if len(nums) == len(path):res.append(path[:])return for i in range(len(nums)):if used[i]:continueif i > 0 and nums[i] == nums[i-1] and not used[i-1]:continuepath.append(nums[i])used[i] = Truedfs()path.pop()used[i] = Falsedfs()return res
總結
以上是生活随笔為你收集整理的Leetcode 47. 全排列 II (每日一题 20211015)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 剑指 Offer 42
- 下一篇: 1. Leetcode 1. 两数之和