Skip to content

Commit 413fd01

Browse files
Pandz18GouravRusiya30
Pandz18
andauthored
Create Permutations solution (#124)
* Create Perm_Recus.java * Update Perm_Recus.java * Updated Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 76c4790 commit 413fd01

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Java/Perm_Recus.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public List<List<Integer>> permute(int[] nums) {
2+
List<List<Integer>> result = new ArrayList<>();
3+
helper(0, nums, result);
4+
return result;
5+
}
6+
7+
private void helper(int start, int[] nums, List<List<Integer>> result)
8+
{
9+
if(start==nums.length-1){
10+
ArrayList<Integer> list = new ArrayList<>(); //if starting value(say n) is the last value , this will make a new list with the values and the new starting value is n
11+
for(int num: nums)
12+
{
13+
list.add(num);
14+
}
15+
result.add(list);
16+
return;
17+
}
18+
19+
for(int i=start; i<nums.length; i++) //a loop to keep on swapping as many times as there are values in the list
20+
{
21+
swap(nums, i, start);
22+
helper(start+1, nums, result);
23+
swap(nums, i, start);
24+
}
25+
}
26+
27+
private void swap(int[] nums, int i, int j)
28+
{
29+
int temp = nums[i]; //temp temporarily holds the value of nums so that i & j values can be exchanged
30+
nums[i] = nums[j];
31+
nums[j] = temp;
32+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
124124
| 189 | [Rotate-Array](https://leetcode.com/problems/rotate-array/) | [Python](./Python/rotate-array.py) | O(N) | O(1) | Medium | Array |
125125
| 496 | [next-greater-element-i](https://leetcode.com/problems/next-greater-element-i) | [Python](./Python/496_nextgreaterelement.py) | O(N) | O(1) | Medium | Array |
126126
| 1470 | [Shuffle the Array](https://leetcode.com/problems/shuffle-the-array) | [Java](./Java/shuffle-the-array.java) | O(N) | O(1) | Easy | Array |
127+
| 124 | [Permutation by Recussion](https://leetcode.com/problems/permutations/) | [Java](./Java/shuffle-the-array.java) | O(N) | O(N) | Easy | Array |
127128
| 283 | [Move-Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./C++/Move-Zeroes.cpp) | O(N) | O(1) | Easy | Array |
128129
| 27 | [Remove-Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) | O(N) | O(1) | Easy | Array |
129-
130130
<br/>
131131
<div align="right">
132132
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)