바위타는 두루미

[leetcode]46. Permutations 본문

Study/Algorithm

[leetcode]46. Permutations

DoRoMii 2019. 8. 10. 10:36
728x90

46. Permutations

 

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]

class Solution(object):
    def Permute(self, cur ,nums, len_nums, result):
        if not nums:
            result.append([i for i in cur])
            return
        
        for i in range(len(nums)) :
            cur.append(nums[i])
            self.Permute(cur, nums[:i]+nums[i+1:], len_nums, result)
            cur.pop()
        return 
    
    def permute(self, nums):
        result = []
        len_nums = len(nums)
        self.Permute([] , nums, len_nums, result)
        return result        
        

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

'Study > Algorithm' 카테고리의 다른 글

[leetcode]79. Word Search  (0) 2019.08.10
[leetcode]78. Subsets  (0) 2019.08.10
[leetcode]22. Generate Parentheses  (0) 2019.08.09
[leetcode]17. Letter Combinations of a Phone Number  (0) 2019.08.09
[leetcode] 200. Number of Islands  (0) 2019.08.09
Comments