Skip to content

Create Permutations solution #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Java/Perm_Recus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
helper(0, nums, result);
return result;
}

private void helper(int start, int[] nums, List<List<Integer>> result)
{
if(start==nums.length-1){
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
for(int num: nums)
{
list.add(num);
}
result.add(list);
return;
}

for(int i=start; i<nums.length; i++) //a loop to keep on swapping as many times as there are values in the list
{
swap(nums, i, start);
helper(start+1, nums, result);
swap(nums, i, start);
}
}

private void swap(int[] nums, int i, int j)
{
int temp = nums[i]; //temp temporarily holds the value of nums so that i & j values can be exchanged
nums[i] = nums[j];
nums[j] = temp;
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 189 | [Rotate-Array](https://leetcode.com/problems/rotate-array/) | [Python](./Python/rotate-array.py) | O(N) | O(1) | Medium | Array |
| 496 | [next-greater-element-i](https://leetcode.com/problems/next-greater-element-i) | [Python](./Python/496_nextgreaterelement.py) | O(N) | O(1) | Medium | Array |
| 1470 | [Shuffle the Array](https://leetcode.com/problems/shuffle-the-array) | [Java](./Java/shuffle-the-array.java) | O(N) | O(1) | Easy | Array |
| 124 | [Permutation by Recussion](https://leetcode.com/problems/permutations/) | [Java](./Java/shuffle-the-array.java) | O(N) | O(N) | Easy | Array |
| 283 | [Move-Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./C++/Move-Zeroes.cpp) | O(N) | O(1) | Easy | Array |
| 27 | [Remove-Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) | O(N) | O(1) | Easy | Array |

<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down