Created
January 31, 2018 01:03
-
-
Save aniruddha84/8a85b28f0cfebf7436348b7d4bda6660 to your computer and use it in GitHub Desktop.
Integer array Permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> permute(int[] nums) { | |
return permute(nums, 0, nums.length - 1); | |
} | |
private List<List<Integer>> permute(int[] nums, int low, int high) { | |
List<List<Integer>> result = new ArrayList<List<Integer>>(); | |
if (low == high) { | |
ArrayList<Integer> temp = new ArrayList<Integer>(); | |
temp.add(nums[low]); | |
result.add(temp); | |
return result; | |
} | |
int curr = nums[low]; | |
List<List<Integer>> permutations = permute(nums, low + 1, high); | |
for(List<Integer> perm: permutations) { | |
perm.add(0, curr); | |
result.add(new ArrayList<Integer>(perm)); | |
for (int i = 0; i < perm.size() - 1; i++) { | |
Integer t = perm.get(i); | |
perm.set(i, perm.get(i+1)); | |
perm.set(i+1, t); | |
result.add(new ArrayList<Integer>(perm)); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment