-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.java
More file actions
24 lines (24 loc) · 772 Bytes
/
Copy pathPermutation.java
File metadata and controls
24 lines (24 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> ds = new ArrayList<>();
boolean freq[] = new boolean[nums.length];
recursive(nums,ans,ds,freq);
return ans;
}
public void recursive(int[] nums,List<List<Integer>> ans,List<Integer> ds,boolean freq[]){
if(ds.size() == nums.length){
ans.add(new ArrayList(ds));
return;
}
for(int i=0; i<nums.length; i++){
if(!freq[i]){
ds.add(nums[i]);
freq[i] = true;
recursive(nums,ans,ds,freq);
ds.remove(ds.size() - 1);
freq[i] = false;
}
}
}
}