当前位置:网站首页>[backtracking] backtracking to solve subset problems
[backtracking] backtracking to solve subset problems
2022-06-12 04:44:00 【Empty city ZA】
- subject : Give you an array of integers nums , Elements in an array Different from each other . Returns all possible subsets of the array ( Power set ).
Solution set You can't Contains a subset of repetitions . You can press In any order Returns the solution set . - Example 1:
Input :nums = [1,2,3]
Output :[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Topic link :https://leetcode-cn.com/problems/subsets/
# Iterative method
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
ress= [[]]
for i in range(len(nums)):
ress += [[nums[i]] + res for res in ress]
return ress
# backtracking
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
path = []
def backtracking(nums, startindex):
res.append(path[:])
for i in range(startindex, len(nums)):
path.append(nums[i])
backtracking(nums,i+1)
path.pop()
backtracking(nums, 0)
return res
subject 2: Give you an array of integers nums , It may contain repeating elements , Please return all possible subsets of the array ( Power set ).
Solution set You can't Contains a subset of repetitions . The solution set returned , Subsets can be classified by In any order array .Input :nums = [1,2,2]
Output :[[],[1],[1,2],[1,2,2],[2],[2,2]]
Topic link :https://leetcode-cn.com/problems/subsets-ii/
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
res = []
path = []
def backtracking(nums, startindex):
res.append(path[:])
for i in range(startindex, len(nums)):
if i > startindex and nums[i] == nums[i-1]:continue
path.append(nums[i])
backtracking(nums, i+1)
path.pop()
nums.sort()
backtracking(nums, 0)
return res
The second question is different in that it is given nums There are duplicate elements in the array , Therefore, it is necessary to de duplicate each layer , But it can be used repeatedly in the depth traversal in the longitudinal direction , It can be understood as looking for 1 All subsets at the beginning 1xx, Look again 2 All subsets at the beginning 2xx, And so on .
Specific explanations can be found in this article https://blog.csdn.net/m0_60370702/article/details/122981954?spm=1001.2014.3001.5502 This article uses backtracking to solve combinatorial problems , But the de duplication method of the last combinatorial problem is the same as that of this subset .
边栏推荐
- QT compiling security video monitoring system 43- picture playback
- JWT learning and use
- What are the black box test case design methods in software testing methods?
- kali_ Change_ Domestic source
- 2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units
- Ebpf series learning (4) learn about libbpf, co-re (compile once – run everywhere) | use go to develop ebpf programs (cloud native tool cilium ebpf)
- Question for the 3D printing lattice?
- leetcode 205. Isomorphic Strings
- Detailed explanation of Command Execution Vulnerability
- [issue 31] 360 background development practice experience - two rounds of technical aspects
猜你喜欢

Understanding of day16 array create query static and dynamic array array array performance in memory

asp. Net core theme Middleware

How to deploy PostgreSQL as a docker container

2022 examination questions and simulation examination for crane driver (limited to bridge crane)

Using datetime in MySQL

QT compiling security video monitoring system 43- picture playback
![Work report of epidemic data analysis platform [6] visual drawing](/img/cc/9eaff451068d0efb174b58719c700e.png)
Work report of epidemic data analysis platform [6] visual drawing

2022 electrician (elementary) operation certificate examination question bank and online simulation examination

Ubunt 20.04 uses CDROM or ISO as the installation source

2022-02-28 WPF upper computer 126 understand mqtt
随机推荐
疫情数据分析平台工作报告【8.5】额外的爬虫和绘图
Shandong University network security range experimental platform -- team and project introduction
Betteland introduces milk products of non animal origin, which will be launched in the U.S. market in the near future
Some points needing attention about thread pool
leetcode797. 所有可能的路径(中等)
【高效】最强开发工具Ctool编译踩坑
Illustrating the use of Apache skywalking UI
Oracle's instr()
MFC General dialog color dialog
Walking "daily question" and "DP"
leetcode 263. Ugly number
Bearpi IOT lighting LED
These programming languages are worth learning
疫情数据分析平台工作报告【1】数据采集
Encapsulation manuelle d'un foreach et d'une carte
Things to challenge
Understanding of day16 array create query static and dynamic array array array performance in memory
请用递归的方法计算下列函数的值:px(x,n)=x-x^2 +x^3- x^4+… ((-1)n-1)(xn) n>0 **输入格式要求:“%lf%d“ 提示信息:“Enter X and N:”
Thousand word masterpiece "programming biography"
JWT learning and use