https://oj.leetcode.com/problems/permutations/
Http://fisherlei.blogspot.com/2012/12/leetcode-permutations.html
Todo
See subset combination, next
Public class solution { // assumes all input numbers are unique public list <list<integer>> permute (Int[] num) { return permute_recursively (num); } //////////////////// // Solution 2: Swap Recursively: // // swap 0-0, 0-1, 0-2, 0-3, .., 0-k // swap 1-1, 1-2, ... 1-k // swap 2-2 ... // ... // swap k-k public list<list<integer>> permute_recursively (Int[] num) {&nBsp; list<list<integer>> result = new arraylist (); perm (0, num, result); return result; } private void perm (int start, int[] num, list<list< Integer>> result) { int Len = num.length; if (Start >= len) { List<Integer> tmp = new ArrayList<> (num.length); for (Int n : num) tmp.add (n); &Nbsp; result.add (TMP); return; } for (int i = start ; i < len ; i ++) { swap (num, start, i); perm ( Start + 1, num , result); swap (num, start, i); } } private void swap (int[] a, int i, int j ) { int tmp = A[i]; a[i] = A[j]; A[j] = tmp; } //////////////////// // solution 1: Insertion: // // n = 1 // 1 // // n = 2 // 2,1 // 1,2 // // n = 3 // 3,2,1 // 2,3,1 // 2,1,3 // 3,1,2 // 1,3,2 // 1,2,3 // // Known n-1 set, for each element, at all possible to insert a number n. // // Bad point: // if the input numbers contains dups, need to use set <Set<Integer>> to avoid dups. public List<List< Integer>> permute_insertion (Int[] num) { list<list<integer>> toreturn = new ArrayList<> (; if ) (num == null || num.length == 0) return toreturn; // insert result when length = 1 toreturn.add (Collections.singletonlist (num[0)); for (int i = 1 ; i < num.length ; i ++) { List<List<Integer>> newresults = New arraylist<> (); for (List<integer> lastr : toreturn) { for (Int j = 0 ; j <= lastr.size () ; j++) { list<integer> newr = new ArrayList<> (LASTR); newr.add (J, num[i]); newresults.add (NEWR) ; } } toreturn = newresults; } return toreturn; }}
[leetcode]46 permutations