LeetCode - Permutations
https://leetcode.com/problems/permutations
Problem
Given an array nums of distinct integers, return all the possible permutations
Solution
There are not too much things to say about this problem, it’s a basic recursive problem.
There are a lot of implementation, i will go with the most basic one. We will try to build all permutations, from left (index 0) to right (index len-1). For each position i, we will pick an “available number” from the given array. An available number is a number that hasn’t been used to build the current permutation from index 0 to position i-1.
Since the array contains distinct integers, we don’t need to care about duplicated permutations.
This is the code:
1 | func permute(nums []int) [][]int { |
Time complexity is O(N!) and also Memory complexity is O(N!)
Support me via: https://ko-fi.com/iamquang95