Created
December 22, 2020 12:16
-
-
Save manojnaidu619/d6844c767101e8255e487600d4b3f1fe to your computer and use it in GitHub Desktop.
Generate all combinations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function subset(nums) { | |
var result = []; | |
dfs(0, [], nums, result); | |
console.log(result); | |
} | |
function dfs(index, path, nums, res) { | |
res.push([...path]) | |
for (var i = index; i < nums.length; i += 1){ | |
path.push(nums[i]); | |
dfs(i + 1, path, nums, res) | |
path.pop(); | |
} | |
} | |
subset([1,2,3,4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment