forked from surajr/CodingInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermute.java
More file actions
23 lines (22 loc) · 691 Bytes
/
permute.java
File metadata and controls
23 lines (22 loc) · 691 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>> result = new ArrayList<>();
backtrack(result, new ArrayList<Integer>(), nums);
return result;
}
public void backtrack(List<List<Integer>> result, List<Integer> temp, int [] nums)
{
if(temp.size() == nums.length)
result.add(new ArrayList<>(temp));
else
{
for(int i=0; i<nums.length; i++)
{
if(temp.contains(nums[i])) continue;
temp.add(nums[i]);
backtrack(result, temp, nums);
temp.remove(temp.size()-1);
}
}
}
}