当前位置:网站首页>[backtracking] full arrangement II leetcode47
[backtracking] full arrangement II leetcode47
2022-06-30 21:46:00 【Breeze_】
Given a sequence that can contain repeating numbers nums , In any order Returns all non repeating permutations . 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]]
link :https://leetcode.cn/problems/permutations-ii
Ideas : and lc46 What's different is , Contains duplicate numbers , here , Learn from array sum II The idea of , First, let's talk about sequence sorting , Then every time you traverse for Loop to determine whether the next number is the same as the previous one , Same auto ignore ( prune ) fall , So as to achieve the goal of weight removal
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
self.ret = []
'''NOTE 1. Thinking about recursive trees ( What are the root and intermediate nodes ) 2. Think about each state of the recursive tree (for The loop condition ) 3. Think about the variables of the recursive tree ( Formal parameters of the backtrace function ) 4. Think about what the leaf nodes of a recursive tree are ( Where to end return And save what ) '''
def helper(nums:list,his:list):
if len(nums)==0:
self.ret.append(his)
return
for idx in range(len(nums)):
if idx!=0 and nums[idx]==nums[idx-1]: # Weight removal tips
continue
helper(nums[:idx]+nums[idx+1:],his+[nums[idx]])
nums.sort()
helper(nums,[])
return self.ret
边栏推荐
猜你喜欢

興奮神經遞質——穀氨酸與大腦健康

Troubleshooting the problem of pytorch geometric torch scatter and torch spark installation errors

Reading notes of Clickhouse principle analysis and Application Practice (2)

Random talk about Clickhouse join

Deployment and use of Nacos

5G 在智慧医疗中的需求

京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条

Summary of errors reported when using YML file to migrate CONDA environment

Clickhouse native monitoring item, system table description

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
随机推荐
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
To the Sultanate of Anderson
全面认识痛风:症状、风险因素、发病机理及管理
笔记【JUC包以及Future介绍】
5g demand in smart medicine
AKK菌——下一代有益菌
How to run jenkins build, in multiple servers with ssh-key
jupyterbook 清空控制台输出
興奮神經遞質——穀氨酸與大腦健康
ceshi deces
Look at the top 10 capabilities of alicloud cipu
Ml & DL: introduction to hyperparametric optimization in machine learning and deep learning, evaluation index, over fitting phenomenon, and detailed introduction to commonly used parameter adjustment
VIM common shortcut keys
Summary of errors reported when using YML file to migrate CONDA environment
1-3 使用SQL管理数据库
《ClickHouse原理解析与应用实践》读书笔记(3)
Deployment and use of Nacos
Ten security measures against unauthorized access attacks
Multi table operation - foreign key constraint
Nacos部署及使用