Permutations

by atalaykutlay

Backtracking solution to "Permutations" problem on Leetcode

Permutations
1class Solution:
2    def permute(self, nums: List[int]) -> List[List[int]]:
3        results = []
4        def backtrack(start_index, used, path):
5            if len(path) == len(nums):
6                results.append(path)
7                return
8
9            for i, num in enumerate(nums):
10                if used[i]:
11                    continue
12                used[i] = True
13                backtrack(start_index + 1, used, path + [num])
14                used[i] = False
15        
16        backtrack(0, defaultdict(bool), [])
17        return results

Share

Share
Video
A cool 10 sec video.
Share
Detailed
Title, description, and syntax highlighted code.
Share
Simple
Syntax highlighted code with gradient background colored presentation.

Comments

It looks like there is no comment for this snippet. Leave the first one!
You need to login to send a comment.

Similar Snippets

Count Number of Texts
Count Number of Texts

Solution to "Count Number of Texts" question on Leetcode.

Language: python12 months ago
Quicksort in Python
Quicksort in Python

An easy quicksort implementation in Python.

Language: python13 months ago
Second Minimum Node In a Binary Tree
Second Minimum Node In a Binary Tree

Solution to "Second Minimum Node In a Binary Tree" on Leetcode.

Language: python11 months ago