forked from riya2001-cloud/java-hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayPermutations.java
36 lines (32 loc) · 1.04 KB
/
ArrayPermutations.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
//Hey everyone, In this problem, You'll be Given an array nums of distinct integers, and have to return all the possible permutations. You can return the answer in any order.
//Example :
//Input: nums = [1,2,3]
//Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
//Below is the code for the above problem
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
int i = 0;
recursion(i, nums);
return res;
}
void recursion(int i, int[] nums) {
if(i == nums.length) {
List<Integer> list = new ArrayList<>();
for(int j : nums) list.add(j);
res.add(new ArrayList<>(list));
return;
}
for(int x = i; x < nums.length; x++){
swap(i, x, nums);
recursion(i+1, nums);
swap(i, x, nums);
}
}
void swap(int i, int x, int[] nums){
int temp = nums[i];
nums[i] = nums[x];
nums[x] = temp;
return;
}
}