当前位置:网站首页>47. Permutations II
47. Permutations II
2022-06-22 12:26:00 【Sterben_Da】
47. Permutations II
Medium
5716100Add to ListShare
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
Example 1:
Input: nums = [1,1,2] Output: [[1,1,2], [1,2,1], [2,1,1]]
Example 2:
Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Constraints:
1 <= nums.length <= 8-10 <= nums[i] <= 10
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
"""
解题思路:与 46.Permutations 不同, 因为存在重复的数字
参考别人的,使用计数器,每次枚举遍历所有key,回溯
遍历计数器的key确保不会出现重复
"""
from collections import Counter
counter = Counter(nums)
result = []
def dfs(l: List[int], count=0):
if count == len(nums):
result.append(l)
else:
for key, c in counter.items():
if c > 0:
counter[key] -= 1
dfs(l + [key], count + 1)
counter[key] += 1
dfs([])
return result边栏推荐
猜你喜欢

ffmpeg将amr格式转成mp3格式

天翼云数字政府智慧数据中台通过认证

Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting

企业官网如何提高客户转化率

0179 largest number

天翼云探索云原生、边缘计算融合新思路

SAP development keys application SSCR keys application

Arcpy adding layers to map documents

Sap-abap- how to find a table and what related tables the fields have

Tis tutorial 02 model
随机推荐
[QT] QT get standard system path
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
老王说系列第六期:PHP程序员要建立自己的自信心
Under Xinchuang: when the domestic database stars shine
SAP system cancels user setting ALV global layout
通过 postgis 制作 按照米制的矩形边框
Terraform的vpc网络自动化配置解决方案
Universaldependencies dependency label interpretation
SiCf batch activation service node
On the routing tree of gin
0007 reverse integer
Isn't this another go bug?
SAP development keys application SSCR keys application
A2L file analysis based on CAN bus (1)
剑指 Offer II 114. 外星文字典
The solution of VPC network automatic configuration based on terraform
PostGIS through St_ Dwithin retrieves elements within a certain distance
天翼云数字政府智慧数据中台通过认证
Isn't the execution process of ODPs SQL executed from top to bottom
Wechat payment QR code generation