-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
58 lines (49 loc) · 1.24 KB
/
Permutations.java
File metadata and controls
58 lines (49 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.java.day01;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* 排列组合问题
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
*/
public class Solution {
public static List<List<Integer>> list=new ArrayList<List<Integer>>();
public static int []flag=new int[100];
//public static int []array=new int [100];
public static int count=0;
public static List<Integer> l=new ArrayList<Integer>();
public static List<List<Integer>> permute(int[] nums) {
fun(nums);
return list;
}
public static void fun(int []array){
if(count<array.length){
for(int j=0;j<array.length;j++){
if(flag[j]==0){
flag[j]=1;
l.add(array[j]);
count++;
fun(array);
flag[j]=0;
count--;
l.remove(j);//不能是l.remove(j),j是不断变化,而remove是顺序的。
//j与count不总是同步的
}
}
}
else{
List<Integer> temp=new ArrayList<Integer>();
temp.addAll(l);
list.add(temp);
//l.clear();
}
}
public static void main(String[] args) {
System.out.println(permute(new int []{1,2,3})) ;
}
}