wherehows
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
블로그 내 검색

wherehows

  • 알고리즘/리트코드

    47. Permutations II

    2021. 6. 20.

    by. 창고

    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

    댓글

    관련글

    • 209. Minimum Size Subarray Sum 2021.07.18
    • 856. Score of Parentheses 2021.07.14
    • 137. Single Number II 2021.06.19
    • 287. Find the Duplicate Number 2021.06.18
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

Today

Total

Powered by ⓒ Kakao Corp.

Designed by Nana
블로그 이미지
창고

티스토리툴바