permutations.dart (893B)
1 //Find and return all possible permutations of a given set of integers 2 //To do this I used recursion to create a decision tree that splits off each time 3 //depending on how many integers in the list are unused. 4 //The time complexity of this code is O(n!) 5 //Time: 276ms Beats: 50% 6 //Memory: 142.7MB Beats: 33.33% 7 8 class Solution { 9 List<List<int>> return_list = []; 10 List<List<int>> permute(List<int> nums) { 11 recurse(nums, []); 12 return return_list; 13 } 14 void recurse(List<int> vals , List<int> current){ 15 if(vals.length == 1){ 16 current.add(vals[0]); 17 return_list.add(current); 18 return; 19 } 20 for(int i = 0 ; i < vals.length ; ++i){ 21 List<int> original = List.from(vals); 22 List<int> curr = List.from(current); 23 curr.add(original[i]); 24 original.removeAt(i); 25 recurse(original, curr); 26 } 27 } 28 }