
g0001_0100.s0046_permutations.Solution.cs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
namespace LeetCodeNet.G0001_0100.S0046_permutations {
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Backtracking
// #Algorithm_I_Day_11_Recursion_Backtracking #Level_2_Day_20_Brute_Force/Backtracking
// #Udemy_Backtracking/Recursion #Big_O_Time_O(n*n!)_Space_O(n+n!)
// #2024_01_11_Time_96_ms_(96.56%)_Space_46.4_MB_(12.40%)
using System.Collections.Generic;
public class Solution {
public IList> Permute(int[] nums) {
if (nums == null || nums.Length == 0) {
return new List>();
}
IList> finalResult = new List>();
PermuteRecur(nums, finalResult, new List(), new bool[nums.Length]);
return finalResult;
}
private void PermuteRecur(
int[] nums, IList> finalResult, List currResult, bool[] used) {
if (currResult.Count == nums.Length) {
finalResult.Add(new List(currResult));
return;
}
for (int i = 0; i < nums.Length; i++) {
if (used[i]) {
continue;
}
currResult.Add(nums[i]);
used[i] = true;
PermuteRecur(nums, finalResult, currResult, used);
used[i] = false;
currResult.RemoveAt(currResult.Count - 1);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy