当前位置:网站首页>Backtracking to solve subset problem
Backtracking to solve subset problem
2022-07-25 02:54:00 【One is dozing】
1. A subset of (leetcode78)
nums Elements do not repeat
There are no termination conditions 、path Update on push Into the res in
var subsets = function(nums) {
let res = [],path= [];
function backtracking(startIndex){
res.push([...path]);
for(let i = startIndex;i<nums.length;i++){
path.push(nums[i]);
backtracking(i+1);
path.pop();
}
}
backtracking(0);
return res;
};2. A subset of II(leetcode90)
nums Elements repeat , It is required that the subset is not repeated
duplicate removal : Use used, De duplication of tree layer
var subsetsWithDup = function(nums) {
nums.sort((a,b)=>a-b);
let used = new Array(nums.length).fill(false);
let res = [],path=[];
function backtracking(startIndex){
res.push([...path]);
for(let i = startIndex;i<nums.length;i++){
if(i>0 && nums[i] == nums[i-1] && !used[i-1]) continue;
if (used[i] == true) continue;
path.push(nums[i]);
used[i] = true;
backtracking(i+1);
path.pop();
used[i] = false;
}
}
backtracking(0);
return res;
};3. Increasing subsequence (leetcode491)
Increasing subsequence
Output : Increasing subsequence in array
Be careful : There may be duplicate elements in the array , So be right path Deduplication , Use used Can also be , Use set Can also be
var findSubsequences = function(nums) {
let res = [],path = [];
const set = new Set();
function backtracking(startIndex){
if(path.length > 1){
// path Array , Convert to string
const str = path.toString();
if(!set.has(str)){
res.push([...path]);
set.add(str);
}
}
for(let i = startIndex;i<nums.length;i++){
if(path.length == 0 || path[path.length-1]<=nums[i]){
path.push(nums[i]);
backtracking(i+1);
path.pop();
}
}
}
backtracking(0);
return res;
};边栏推荐
- BGP introduction
- JS foundation -- task queue and event loop
- Conceptual distinction between Po, Bo, VO, dto and POJO
- Tp5.1 paging (with parameter transfer)
- 【C】 Advanced knowledge of file operation
- Arduino + si5351 square wave generator
- Threat report in June: new bank malware malibot poses a threat to mobile banking users
- "Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?
- JS written test -- regular expression
- On Calc optimization of calcite
猜你喜欢

Permanently mount the image steps

Is redis'module'not an internal or external command?

Tensorflow's study notes (I)

Pypi counts the number of Downloads

Wechat sports field reservation of the finished works of the applet graduation project (7) mid-term inspection report

Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem

Is it necessary to increase the number of milliseconds and save several KB of memory in the program?

Vulntarget vulnerability shooting range -vulntarget-b

Jenkins plug-in development -- plug-in expansion

Get to know string thoroughly
随机推荐
Flink's study notes
Technical experts from large factories: Thoughts on cloud nativity and software supply chain security
Operator explanation - C language
Details of C language compilation preprocessing and comparison of macros and functions
JS written test question -- deep copy of object
UDP message structure and precautions
Mid year summary and personal feelings
[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem
Domain driven model (DDD)
Jenkins plug-in development -- plug-in expansion
Tp5.0 background admin access
Method of adding kernel in Jupiter notebook
Redis unauthorized access vulnerability recurrence (www.hetianlab.com)
Permanently mount the image steps
Generator set work arrangement problem code
Study notes of filebeat
JS written test question -- prototype, new, this comprehensive question
TS uses a third-party library, and there is no type declaration file error handling
Digital business cloud: how to realize the application value of supplier SRM management system?