Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 791 Bytes

46.-permutations1.md

File metadata and controls

33 lines (26 loc) · 791 Bytes
description
Back-tracking

46. Permutations1

  • Medium
  • Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Analysis

This is also a back-tracking problem.

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        ans=[]
        answer=[]
        self.helper(nums,ans,answer)
        return answer
        
    def helper(self,candidates, ans,answer):
        if not candidates:
            answer.append(ans)
            return
        for i in candidates:
            temp=candidates.copy()
            temp.remove(i)
            #print(ans)
            self.helper(temp,ans+[i],answer)