bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 46. Permutations

46. Permutations

Leetcode Solutions

Backtracking to Generate Permutations

  1. Define a helper function backtrack that takes the current permutation curr and the list of numbers nums.
  2. If the length of curr is equal to the length of nums, add a copy of curr to the answer list ans.
  3. Iterate over each number in nums.
  4. If the number is not in curr, add it to curr and call backtrack recursively.
  5. After the recursive call, remove the last element from curr to backtrack.
  6. Call backtrack initially with an empty curr.
  7. Return the answer list ans containing all permutations.
UML Thumbnail

Iterative Swap to Generate Permutations

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...