반응형
1. 문제
https://leetcode.com/problems/subsets/description/
2. 코드
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums) {
let answer = [];
const dfs = (fixed, left) => {
if(!answer.includes(fixed)) answer.push(fixed);
for(let i=0;i<left.length;i++){
if(!fixed.length || left[i] > fixed[fixed.length-1]){
let newFixed = [...fixed,left[i]];
let newLeft = left.slice();
newLeft.splice(i,1);
dfs(newFixed,newLeft);
}
}
}
dfs([],nums);
return answer.sort((a,b) => a.length-b.length);
};
반응형
'리트코드 > midium' 카테고리의 다른 글
[리트코드] 39. Combination Sum - js (0) | 2023.08.07 |
---|---|
[리트코드] 230. Kth Smallest Element in a BST - js (0) | 2023.07.20 |
[리트코드] 48. Rotate Image - js (0) | 2023.07.14 |
[리트코드] 22. Generate Parentheses - js (0) | 2023.07.13 |
[리트코드] 46. Permutations - js (0) | 2023.07.08 |