Lecture number 39 permutation code not available
Closed this issue · 1 comments
vinitbolla commented
vinitbolla commented
i have got the code for the above issue which beats the tc by 100% and sc by 76%
code:
class Solution {
private void solve(int[] nums, int index, List<List> res){
if(index >= nums.length){
List list = new ArrayList();
for(int num: nums) list.add(num);
res.add(list);
return;
}
for(int j = index; j < nums.length; j++){
int temp = nums[j];
nums[j] = nums[index];
nums[index] = temp;
solve(nums,index+1,res);
temp = nums[j];
nums[j] = nums[index];
nums[index] = temp;
}
}
public List<List> permute(int[] nums) {
List<List> res = new ArrayList<>();
if(nums.length == 0){
return res;
}
int index = 0;
solve(nums,index,res);
return res;
}
}