forked from AsCE13/hack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
23 lines (23 loc) · 806 Bytes
/
Permutations.cpp
File metadata and controls
23 lines (23 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
boolean[] visited = new boolean[nums.length];
dfs(nums, res, new LinkedList<Integer>(), visited);
return res;
}
private void dfs(int[] nums, List<List<Integer>> res, List<Integer> curr, boolean[] visited) {
if (curr.size() == nums.length) {
res.add(new LinkedList<Integer>(curr));
return;
}
for (int i = 0; i < nums.length; i ++) {
if (visited[i] == false) {
visited[i] = true;
curr.add(nums[i]);
dfs(nums, res, curr, visited);
curr.remove(curr.size() - 1);
visited[i] = false;
}
}
}
}