-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek 4 46. Permutations
More file actions
29 lines (27 loc) · 903 Bytes
/
week 4 46. Permutations
File metadata and controls
29 lines (27 loc) · 903 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
25
26
27
28
29
public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums == null || nums.length == 0){
return res;
}
helper(res, new ArrayList<Integer>(), nums, new boolean[nums.length]);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> path, int[] nums, boolean[] isVisited){
if(path.size() == nums.length){
res.add(new ArrayList<Integer>(path));
return;
}
for(int i = 0; i < nums.length; i++){
if(isVisited[i]){
continue;
}
path.add(nums[i]);
isVisited[i] = true;
helper(res, path, nums, isVisited);
path.remove(path.size() - 1);
isVisited[i] = false;
}
return;
}
}