forked from fineanmol/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46. Permutations.txt
More file actions
31 lines (26 loc) · 830 Bytes
/
46. Permutations.txt
File metadata and controls
31 lines (26 loc) · 830 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
30
31
Leetcode - 46. Permutations
https://leetcode.com/problems/permutations/
class Solution {
List<List<Integer>> arr=new LinkedList<List<Integer>>();
public List<List<Integer>> permute(int[] n) {
List<Integer> ar=new LinkedList<Integer>();
boolean b[]=new boolean[n.length];
traversal(ar,b,n);
return arr;
}
public void traversal(List<Integer> ar,boolean[] vis, int[] n){
if(ar.size()==n.length){
arr.add(new LinkedList<>(ar));
return;
}
for(int i=0;i<vis.length;i++){
if(!vis[i]){
vis[i]=true;
ar.add(n[i]);
traversal(ar,vis,n);
vis[i]=false;
ar.remove(ar.size()-1);
}
}
}
}