forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations.java
28 lines (25 loc) · 859 Bytes
/
Permutations.java
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
// Runtime: 2 ms (Top 76.85%) | Memory: 44.7 MB (Top 48.41%)
class Solution {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
ArrayList<Integer> list = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
backTrack(nums, list, visited);
return res;
}
private void backTrack(int[] nums, ArrayList<Integer> list, boolean[] visited){
if(list.size() == nums.length){
res.add(new ArrayList(list));
return;
}
for(int i = 0; i < nums.length; i++){
if(!visited[i]){
visited[i] = true;
list.add(nums[i]);
backTrack(nums, list, visited);
visited[i] = false;
list.remove(list.size() - 1);
}
}
}
}