Then, 2 can be added in front or after 1. So we have to copy the List<Integer> in answer (it's just {1}), add 2 in position 0 of {1}, then copy the original {1} again, and add 2 in position 1. Now we have an answer of {2,1},{1,2}. There are 2 lists in the current answer.
Then we have to add 3. first copy {2,1} and {1,2}, add 3 in position 0; then copy {2,1} and {1,2}, and add 3 into position 1, then do the same thing for position 3.
publicclassSolution{publicList<List<Integer>>permute(int[]nums){List<List<Integer>>ret=newArrayList<List<Integer>>();List<Integer>l0=newArrayList<Integer>();if(nums==null||nums.length==0)returnret;l0.add(nums[0]);ret.add(l0);for(inti=1;i<nums.length;i++){List<List<Integer>>new_ret=newArrayList<List<Integer>>();// j: new posfor(intj=0;j<=i;j++){// each list in retfor(List<Integer>l:ret){// copy listList<Integer>new_l=newArrayList<Integer>(l);new_l.add(j,nums[i]);new_ret.add(new_l);}}ret=new_ret;}returnret;}}