반응형
1. 문제
https://leetcode.com/problems/permutations/
2. 코드
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
let answer = [];
const dfs = (fixed,left) => {
if(fixed.length === nums.length) answer.push(fixed);
else{
for(let i=0;i<left.length;i++){
let newFixed = fixed.slice();
newFixed = [...fixed,left[i]];
let newLeft = left.slice();
newLeft.splice(i,1);
if(!answer.includes(newFixed)) dfs(newFixed,newLeft);
}
}
}
dfs([],nums);
return answer;
};
반응형
'리트코드 > 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 |
[리트코드] 78. Subsets - js (0) | 2023.07.08 |