题解 | #有重复项数字的全排列#
有重复项数字的全排列
http://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
代码优化空间很大
import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
permute1(num,0);
return new ArrayList<>(set);
}
public void permute1(int[] num,int i) {
if(i==num.length){
ArrayList<Integer> integers = new ArrayList<>();
for (int i1 : num) {
integers.add(i1);
}
set.add(integers);
return;
}
for (int j = i; j < num.length; j++) {
swap(num,j,i);
permute1(num,i+1);
swap(num,j,i);
}
}
public void swap(int[] arr,int s,int e){
int temp = arr[s];
arr[s] = arr[e];
arr[e] = temp;
}
TreeSet<ArrayList<Integer>> set = new TreeSet<>((a1,a2)->{
int i = 0;
while(i<a1.size()){
if(a1.get(i).compareTo(a2.get(i))!=0){
return a1.get(i).compareTo(a2.get(i));
}else{
i++;
}
}
return 0;
});
}