当前位置:网站首页>[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
边栏推荐
- vim 常用快捷键
- 盘点华为云GaussDB(for Redis)六大秒级能力
- CA I ah, several times Oh, ah, a sentence IU home Oh
- jupyterbook 清空控制台输出
- Sqlserver string type converted to decimal or integer type
- Side sleep ha ha ha
- 興奮神經遞質——穀氨酸與大腦健康
- What does grade evaluation mean? What is included in the workflow?
- 1-21 JSONP接口
- Phoenix architecture: an architect's perspective
猜你喜欢
随机推荐
VIM common shortcut keys
1-2 install and configure MySQL related software
Fletter nested hell? No, constraintlayout to save!
jupyter notebook/lab 切换conda环境
Sqlserver gets the data of numbers, Chinese and characters in the string
CA I ah, how many times Oh, ah sentence IU home Oh 11111
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
1-11 create online file service
Auto-created primary key used when not defining a primary key
Go Web 编程入门: 一探优秀测试库 GoConvey
给苏丹国安德森苏丹的撒过 d s g
看阿里云 CIPU 的 10 大能力
sdfsdf
兴奋神经递质——谷氨酸与大脑健康
. NETCORE redis geo type
Is it safe to open an account for stock trading on mobile phones?
5G 在智慧医疗中的需求
Who are you and I
sdfsdf
jupyterbook 清空控制台输出








