-
https://leetcode.com/problems/permutations-ii/
Permutations II - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
순열구하기
과일 바구니를 떠올리자. 과일은 배열의 요소들이고 이를 바구니(객체)에 담는다. 그리고 과일 개수만큼 준비된 접시에 차례대로 올려놓는다고 생각하자.
https://leetcode.com/problems/permutations-ii/discuss/893745/Very-Easy-Recursive-JS-Solution
var permuteUnique = function(nums) { let basket = {}; let res = []; let set = new Set(); for(let i = 0; i < nums.length; i++) { set.add(nums[i]); basket[nums[i]] = basket[nums[i]] ? basket[nums[i]] + 1 : 1; } function search(arr) { if(arr.length === nums.length) { res.push(arr); return; } for(let val of set) { if(basket[val] !== 0) { basket[val]--; search([...arr, val]); basket[val]++; } else if(basket[val] === 0) { continue; } } } search([]); return res; };
'알고리즘 > 리트코드' 카테고리의 다른 글
209. Minimum Size Subarray Sum (0) 2021.07.18 856. Score of Parentheses (0) 2021.07.14 137. Single Number II (0) 2021.06.19 287. Find the Duplicate Number (0) 2021.06.18 153. Find Minimum in Rotated Sorted Array (0) 2021.06.17 댓글