-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathPermutations.java
36 lines (29 loc) · 1.12 KB
/
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
29
30
31
32
33
34
35
36
//LeetCode 46. Permutation
//Question - https://leetcode.com/problems/permutations/
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
boolean visit[] = new boolean[nums.length];
helper(nums, visit, new ArrayList<>(), res);
return res;
}
public void helper(int nums[], boolean visit[], List<Integer> perm, List<List<Integer>> res){
//Base Case
if(perm.size() == nums.length){
res.add(new ArrayList<>(perm));
return;
}
for(int i = 0 ; i < nums.length ; i++){
//Check if nums[i] is already in the current permutation or not
if(visit[i]) continue;
//mark nums[i] as visited for the current permutation
visit[i] = true;
perm.add(nums[i]);
//generate the permutation further
helper(nums, visit, perm, res);
//backtrack to generate new permutation
visit[i] = false;
perm.remove(perm.size()-1);
}
}
}