Permutation problem backtracking - US Algorithms
Given a no repeat sequence of numbers, it returns all possible full array.
Example:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Permutation {
static List<List<Integer>> res;
static int[] nums;
static boolean[] used;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
nums = new int[str.length()];
used = new boolean[str.length()];
for(int i=0;i<str.length();i++) {
char num = str.charAt(i);
nums[i] = num-'0';
nums[i] =Integer.valueOf(num-'0').intValue();
}
res = new ArrayList<>();
generatePermutation(nums,0,new LinkedList<>());
System.out.println(res.toString());
}
private static void generatePermutation(int[] nums, int index, LinkedList<Integer> p) {
// TODO Auto-generated method stub
if(index==nums.length) {
res.add((List<Integer>) p.clone());
return;
}
for(int i=0;i<nums.length;i++) {
if(!used[i]) {
used[i]=true;
p.addLast(nums[i]);
generatePermutation(nums,index+1,p);
p.removeLast();
used[i]=false;
}
}
return;
}
}
Output:
Given a sequence of numbers may include repeat, return all distinct full array.
Example:
输入: [1,1,2]
输出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
The basic difficulty is to remove repeated
with this [1,1,2] Example
normal return results 6: wherein the first line is labeled 10 starts, while the second line is marked as the start 11 .
1,1,2; 1,2,1
1,1,2; 1,2,1
2,1,1; 2,1,1
deduplication practice is, when i is determined to dfs and i-1 are equal and i 1-if this value is being used.
Are equal and are not used in this case i is skipped, directly to i + 1 is determined.
Because not being used words, then you can reuse the i-1, it will repeat the situation.
For example, the second line is determined at i = 1, this time is equal to the value of i and i-1 and i-1 and the value is not used
is skipped in this case i = 1, directly to i = 2, we to remove all duplicate the second row.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class PermutationII {
static List<List<Integer>> res;
static int[] nums;
static boolean[] used;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
nums = new int[str.length()];
used = new boolean[str.length()];
for(int i=0;i<str.length();i++) {
char num = str.charAt(i);
nums[i] = num-'0';
nums[i] =Integer.valueOf(num-'0').intValue();
}
Arrays.sort(nums);
res = new ArrayList<>();
generatePermutation(nums,0,new LinkedList<>());
System.out.println(res.toString());
}
private static void generatePermutation(int[] nums, int index, LinkedList<Integer> p) {
// TODO Auto-generated method stub
if(index==nums.length) {
res.add(new ArrayList<>(p));
return;
}
for(int i=0;i<nums.length;i++) {
if(used[i]||(i>0&&nums[i-1]==nums[i]&&used[i-1]==false)) {
continue;
}
if(!used[i]) {
used[i]=true;
p.addLast(nums[i]);
generatePermutation(nums,index+1,p);
p.removeLast();
used[i]=false;
}
}
return;
}
}
Output: