class Solution: #number of permutations = n! def permute(self, nums: List[int]) -> List[List[int]]: result = [] if len(nums) == 1: return [nums[:]] for i in range(len(nums)): #pop off first element n = nums.pop(0) #numswill now have one less value, will append popped value later perms = self.permute(nums) for perm in perms: perm.append(n) result.extend(perms) #adding popped back nums.append(n) return result
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter