-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
37 lines (35 loc) · 853 Bytes
/
Copy pathsolution.java
File metadata and controls
37 lines (35 loc) · 853 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
32
33
34
35
36
37
// 46. Permutations
// https://leetcode.com/problems/permutations/
// Medium | Java | Accepted 2025-12-14
// Runtime 2 ms | Memory 45.4 MB
class Solution {
List<List<Integer>> ans = new ArrayList<>();
int[] num;
public List<List<Integer>> permute(int[] nums) {
num = nums;
recurse(0, new ArrayList<>());
return ans;
}
public void recurse(int ind, List<Integer> temp)
{
if(ind>=num.length)
{
if(temp.size()==num.length)
{
ans.add(new ArrayList<>(temp));
}
return;
}
for(int i = 0; i<num.length; i++)
{
if(temp.contains(num[i]))
{
continue;
}
temp.add(num[i]);
recurse(ind+1, temp);
temp.remove(temp.size()-1);
}
return;
}
}