Skip to main content

One post tagged with "backtracking"

View All Tags

· One min read

https://leetcode.com/problems/permutations/

Solution 1

/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
const results = [];

const permutations = (selected) => {
// nums의 모든 숫자가 다 쓰였을 경우
if (selected.length === nums.length) {
results.push(selected);
return;
}

for (let i = 0; i < nums.length; i++) {
// 중복인 숫자는 제외하고 추가
if (!selected.includes(nums[i])) {
permutations([...selected, nums[i]]);
}
}
};

permutations([]);

return results;
};