An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
April 26, 2021 01:56 am GMT
Original Link: https://dev.to/cod3pineapple/leetcode-46-permutations-javascript-solution-4552
LeetCode 46. Permutations (javascript solution)
Description:
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Solution:
Time Complexity : O(n!)
Space Complexity: O(n)
var permute = function(choices, temp = [], permutations = []) { // Base case if(choices.length === 0){ permutations.push([...temp]); } for(let i = 0; i < choices.length; i++){ // Create new array without current letter let newChoices = choices.filter((choice, index) => index !== i) // Add current to the temp array which is our current permutation temp.push(choices[i]) permute(newChoices, temp, permutations) // Once we have explored options remove the current letter from our current permuataion temp.pop() } return permutations};
Original Link: https://dev.to/cod3pineapple/leetcode-46-permutations-javascript-solution-4552
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To