Permutations
Given A collection of numbers, return all possible permutations.
For example,
[following] has the permutations:
[1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
Solution 1:
Classic recursive backtracking topic, one accept. Please also refer to the previous topic Leetcode:combinations problem Solving report.
1 Public classSolution {2 PublicList<list<integer>> Permute (int[] num) {3list<list<integer>> ret =NewArraylist<list<integer>>();4 if(num = =NULL|| Num.length = = 0) {5 returnret;6 }7 8DFS (NUM,NewArraylist<integer>(), ret);9 returnret;Ten } One A Public voidDfsint[] num, list<integer> path, list<list<integer>>ret) { - intLen =num.length; - if(path.size () = =Len) { theRet.add (NewArraylist<integer>(path)); - return; - } - + for(inti = 0; i < Len; i++) { - if(Path.contains (Num[i])) { + Continue; A } at - Path.add (Num[i]); - dfs (num, path, ret); -Path.remove (Path.size ()-1); - } - } in}
View Code
Solution 2:
Maybe some students think why path.contains do not hashmap to replace the miles? So the homepage June wrote a version with HashMap. The conclusion is that when the set size is small, the performance of HashMap is not
such as ArrayList.
The reason may be that the HashMap application is not a continuous space, and ArrayList relatively small, directly in continuous memory operation, the speed will be relatively fast.
The following is the result of running this program, and the version of HashMap is one times slower:
Test Size:9
Computing time with hashmap:629.0 millisec.
Test Size:9
Computing time with list:310.0 millisec.
1 Packagealgorithms.permutation;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashSet;5 ImportJava.util.LinkedHashMap;6 7 Public classPermutation {8 Public Static voidMain (string[] strs) {9 int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9};TenSystem.out.printf ("Test size:%d \ n", num.length); One AStopwatch timer1 =NewStopwatch (); - - permute (num); the System.out -. println ("Computing Time with HASHMAP:" -+ timer1.elapsedtime () + "millisec."); - +System.out.printf ("Test size:%d \ n", num.length); - +Stopwatch Timer2 =NewStopwatch (); A at permute2 (num); - System.out -. println ("Computing Time with List:" -+ timer2.elapsedtime () + "millisec."); - } - in Public StaticArraylist<arraylist<integer>> Permute (int[] num) { -arraylist<arraylist<integer>> ret =NewArraylist<arraylist<integer>>(); to if(num = =NULL) { + returnret; - } the *Permutehelp (NUM, ret,NewLinkedhashmap<integer, integer>()); $ returnret;Panax Notoginseng } - the Public Static voidPermutehelp (int[] num, arraylist<arraylist<integer>> ret, Linkedhashmap<integer, integer>set) { + if(set.size () = =num.length) { A thearraylist<integer> list =NewArraylist<integer>(); + for(Integer i:set.keyset ()) { - List.add (i); $ } $ Ret.add (list); - return; - } the - intLen =num.length;Wuyi for(inti = 0; i < Len; i++) { the if(Set.containskey (Num[i])) { - Continue; Wu } - About //Path.add (Num[i]); $Set.put (Num[i], 0); - permutehelp (num, ret, set); - //Path.remove (Path.size ()-1); - Set.remove (Num[i]); A } + } the - Public StaticArraylist<arraylist<integer>> Permute2 (int[] num) { $arraylist<arraylist<integer>> ret =NewArraylist<arraylist<integer>>(); the if(num = =NULL) { the returnret; the } the -arraylist<integer> Path =NewArraylist<integer>(); in permuteHelp2 (num, path, ret); the returnret; the } About the Public Static voidPERMUTEHELP2 (int[] num, arraylist<integer> path, arraylist<arraylist<integer>>ret) { the if(path.size () = =num.length) { theRet.add (NewArraylist<integer>(path)); + return; - } the Bayi intLen =num.length; the for(inti = 0; i < Len; i++) { the if(Path.contains (Num[i])) { - Continue; - } the the Path.add (Num[i]); the permuteHelp2 (num, path, ret); thePath.remove (Path.size ()-1); - } the } the}
GITHUB:
Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dfs/Permute.java
Leetcode:permutations Problem Solving Report