loveBabbar/CodeHelp-DSA-Busted-Series

Lecture number 39 permutation code not available

Closed this issue · 1 comments

Hello Team,

Can you please add your code for lecture number 39 Permutation. I was coding in java but the solution's time compl. is more. It is only beating 6% users solution.

image

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;
}
}