88 * @Date: 2020/7/14 上午9:36
99 * @Package: com.leosanqing.leetcode.medium.array
1010 * @Description: 1
11- *` Given a collection of distinct integers, return all possible permutations.
12- *` Example:
13- *` Input: [1,2,3]
14- *` Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
11+ * ` Given a collection of distinct integers, return all possible permutations.
12+ * ` Example:
13+ * ` Input: [1,2,3]
14+ * ` Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
1515 * @Version: 1.0
1616 */
1717public class _46_permutations {
@@ -20,37 +20,29 @@ public static void main(String[] args) {
2020 System .out .println (permute (new int []{1 , 2 , 3 }));
2121 }
2222
23-
2423 public static List <List <Integer >> permute (int [] nums ) {
2524 List <List <Integer >> answer = new ArrayList <>();
2625
27- backTrace (answer , new ArrayList <>(), nums );
26+ backtrace (answer , nums , new ArrayList <>());
2827 return answer ;
29-
3028 }
3129
32- /**
33- * 递归遍历
34- * @param answer
35- * @param list
36- * @param nums
37- */
38- private static void backTrace (List <List <Integer >> answer , List <Integer > list , int [] nums ) {
30+
31+ private static void backtrace (List <List <Integer >> answer , int [] nums , List <Integer > list ) {
3932 if (list .size () == nums .length ) {
4033 answer .add (new ArrayList <>(list ));
4134 return ;
4235 }
36+
4337 for (int num : nums ) {
44- // 如果有这个数字,就跳过
4538 if (list .contains (num )) {
4639 continue ;
4740 }
41+
4842 list .add (num );
49- backTrace (answer , list , nums );
43+ backtrace (answer , nums , list );
5044 list .remove (list .size () - 1 );
5145 }
52-
53-
5446 }
5547
5648}
0 commit comments