Skip to content

Instantly share code, notes, and snippets.

@nma
Created April 6, 2019 04:14
Show Gist options
  • Save nma/68b72992e7b87908fba674b8e719a280 to your computer and use it in GitHub Desktop.
Save nma/68b72992e7b87908fba674b8e719a280 to your computer and use it in GitHub Desktop.
Permutations Backtracking
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
result = []
def backtracking(permutations, path, options):
if len(path) == len(options):
permutations.append(path[:])
else:
for option in options:
if option in path:
continue
path.append(option)
backtracking(permutations, path, options)
path.pop()
backtracking(result, [], nums)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment