Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Debasis B
C# solution recursive DFS

Debasis B

Feb 2, 2025

// List to store all permutations List<List<int>> result = new List<List<int>>(); public List<List<int>> findPermutations(int[] nums) { // Start the recursive permutation finding process findPermutations(nums, currIndex: 0, currPermutation: new List<int>()); return result; } private void findPermutations(int[] nums, int currIndex, List<int> currPermutation) { // Base case: if the current index is beyond the last index, add the permutation to the result if (currIndex > nums.Length - 1) { result.Add(new List<int>(currPermutation)); return; } // Insert the current element at all possible positions in the current permutation for (int i = 0; i < currIndex + 1; i++) { currPermutation.Insert(i, nums[currIndex]); // Recursively find permutations with the next element findPermutations(nums, currIndex + 1, currPermutation); // Remove the element to backtrack and try the next position currPermutation.RemoveAt(i); } }

0

0

Comments
Comments

On this page