public class Solution { public IList> Permute(int[] nums) { return Collect(nums.ToList()); } private IList > Collect(IList nums) { if(nums.Count <= 1){ return new List >(){new List (nums)}; } if(nums.Count == 2){ return new List >(){new List (){nums[0],nums[1]},new List (){nums[1],nums[0]}}; } var newSet = new List >(); for(var i = 0;i < nums.Count; i++){ // take out nums[i] and put at last for each sub set var x = nums[i]; nums.RemoveAt(i); var subSet = Collect(nums); for(var k = 0;k < subSet.Count; k++){ subSet[k].Add(x); newSet.Add(subSet[k]); } nums.Insert(i, x); } return newSet; } }