leetcode-回溯(JS实现)
2021/10/11 6:18:04
本文主要是介绍leetcode-回溯(JS实现),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
46. 全排列
/** * @param {number[]} nums * @return {number[][]} */ var permute = function(nums) { const res = []; const used = {}; function dfs(path) { if(path.length === nums.length){ res.push(path.slice()); return res; } for(let i = 0; i < nums.length; i++) { if(used[i]) continue path.push(nums[i]); used[i] = true; dfs(path); path.pop(); used[i] = false; } } dfs([]); return res; };
47. 全排列 II
/** * @param {number[]} nums * @return {number[][]} */ var permuteUnique = function(nums) { let res = []; let used = {}; nums.sort((a,b)=>a-b); const dfs = (path) => { if(path.length === nums.length){ res.push(path.slice()); return res; } for(let i = 0; i < nums.length; i++) { if(used[i]) continue if(i-1>=0 && nums[i] === nums[i-1] && !used[i-1]) continue; path.push(nums[i]); used[i] = true; dfs(path); path.pop(); used[i] = false; } } dfs([]); return res; };
39. 组合总和
/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */ var combinationSum = function(candidates, target) { let res = []; const dfs = (start, temp, sum) => { if(sum >= target){ if(sum === target){ res.push(temp.slice()); } return; } for(let i = start; i < candidates.length; i++){ temp.push(candidates[i]); dfs(i, temp, sum + candidates[i]); temp.pop(); } } dfs(0,[],0); return res; };
40. 组合总和 II
/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */ var combinationSum2 = function(candidates, target) { let res = []; candidates.sort((a,b)=>a-b); const dfs = (start,temp,sum)=>{ if(sum>=target){ if(sum === target){ res.push(temp.slice()); } return; } for(let i = start; i < candidates.length; i++){ if(i-1>=start && candidates[i] === candidates[i-1]){ continue; } temp.push(candidates[i]); dfs(i+1, temp, sum + candidates[i]); temp.pop(); } } dfs(0,[],0); return res; };
216. 组合总和 III
/** * @param {number} k * @param {number} n * @return {number[][]} */ var combinationSum3 = function(k, n) { let res = []; const dfs = (start,temp,sum) => { if(temp.length === k) { if(sum === n) { res.push(temp.slice()); } return; } for(let i = start; i <= 9; i++){ temp.push(i); dfs(i+1,temp,sum + i); temp.pop(); } } dfs(1,[],0); return res; };
78. 子集
/** * @param {number[]} nums * @return {number[][]} */ var subsets = function(nums) { let res=[]; const dfs = (start, temp) => { res.push(temp.slice()); for(let i = start; i < nums.length; i++){ temp.push(nums[i]); dfs(i+1, temp); temp.pop(); } }; dfs(0,[]); return res; };
90. 子集 II
/** * @param {number[]} nums * @return {number[][]} */ var subsetsWithDup = function(nums) { let res=[]; nums.sort((a,b)=>a-b); const dfs = (start,temp) => { res.push(temp.slice()); for(let i = start; i < nums.length; i++){ if(i-1 >= start && nums[i-1]===nums[i]){ continue; } temp.push(nums[i]); dfs(i+1,temp); temp.pop(); } } dfs(0,[]); return res; };
这篇关于leetcode-回溯(JS实现)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-21Vue3教程:新手入门到实践应用
- 2024-12-21VueRouter4教程:从入门到实践
- 2024-12-20Vue3项目实战:从入门到上手
- 2024-12-20Vue3项目实战:新手入门教程
- 2024-12-20VueRouter4项目实战:新手入门教程
- 2024-12-20如何实现JDBC和jsp的关系?-icode9专业技术文章分享
- 2024-12-20Vue项目中实现TagsView标签栏导航的简单教程
- 2024-12-20Vue3入门教程:从零开始搭建你的第一个Vue3项目
- 2024-12-20从零开始学习vueRouter4:基础教程
- 2024-12-20Vuex4课程:新手入门到上手实战全攻略