λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸƒ algorithm/leetcode

LeetCode 46 - Permutations (Medium)

by HandHand 2021. 3. 3.

문제

LeetCode - 46번

풀이 κ³Όμ •

주어진 λ°°μ—΄λ‘œ μˆœμ—΄μ„ λ§Œλ“œλŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

λ°±νŠΈλž˜ν‚Ή 을 톡해 λ°°μ—΄μ˜ μš”μ†Œλ“€μ„ ν•˜λ‚˜μ”© μˆœνšŒν•˜λ©° μˆœμ—΄μ„ λ§Œλ“€μ–΄μ£Όλ©΄ λ©λ‹ˆλ‹€.
μ΄λ•Œ λ°°μ—΄ μ—°μ‚°μœΌλ‘œ push or pop 을 μ‚¬μš©ν•˜μ§€ μ•Šκ³  javascript 의 spread μ—°μ‚°μžλ‘œ
μƒˆλ‘œμš΄ 배열을 λ§Œλ“€μ–΄ λ„˜κ²¨μ£ΌλŠ” 방식을 μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

μ½”λ“œ

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

  function permutation(selected) {
    if (selected.length === nums.length) {
      ans.push(selected);
      return;
    }

    for (let i = 0; i < nums.length; i++) {
      if (!selected.includes(nums[i])) {
        permutation([...selected, nums[i]]);
      }
    }
  }

  permutation([]);

  return ans;
};
λ°˜μ‘ν˜•

'πŸƒ algorithm > leetcode' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

LeetCode 347 - Top K Frequent Elements (Medium)  (0) 2021.03.03
LeetCode 322 - Coin Change (Medium)  (0) 2021.03.03
LeetCode 21 - Merge Two Sorted Lists (Easy)  (0) 2021.03.02
LeetCode 240 - Search a 2D Matrix II (Medium)  (0) 2021.03.02
LeetCode 155 - Min Stack (Easy)  (0) 2021.03.02

πŸ’¬ λŒ“κΈ€