public class Test2 {
@Test
public void test(){
int[] arr = {2,3,1};
List<String> list = permute(arr);
System.out.println(list);
System.out.println(Arrays.toString(arr));
}
private List<String> permute(int[] arr) {
return permute0(arr,0,new ArrayList<>());
}
private List<String> permute0(int[] arr,int now,List<String> rs) {
if(now == arr.length) {
rs.add(Arrays.toString(arr));
}
for (int i = now; i < arr.length; i++) {
swap(arr,now,i);
permute0(arr,now+1,rs);
swap(arr,now,i);
}
return rs;
}
private void swap(int[] arr,int i,int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}