Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
.
==========
Analysis:
Tried an example [1,2,3,4]:
Therefore, we need a for loop to process all the possibilities in each element of the array. For example, array[0] can be 1, 2, 3, 4. While in the case of array[0] = 1, array[1] can be 2, 3, 4. Recursively doing this we can get all the possible permutations.
However, I failed to implement this idea by myself. After comparing my solution with the ones that are available in Internet. There are some key points of the implementation.
1. Create a new array "visited[num.size()]" to keep the which element of the original array has been visited, so as to ensure only the remaining elements will be processed. For example, in case of array[0] = 1, only 2,3,4 can be process for array[1].
2. Remove the last element from the array, and resume the visit flag in order to process next possible permutation. For example, after having [1, 2, 3, 4], remove 4 from array (array will be fallen back to [1, 2, 3]), and reset visit flag of the 3rd element to un-visited. Then go into the next iteration: put 4 into the array. New array this time would be [1, 2, 4].
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> element = new ArrayList<Integer>();
boolean[] visited = new boolean[num.length];
helper(num, result, element, visited);
return result;
}
public void helper(int[] num, ArrayList<ArrayList<Integer>> result, ArrayList<Integer> element, boolean[] visited){
if (element.size() == num.length){
// duplicate element and add it to result (element would be changed from time to time. If directly use element
// only result would be changed when element changed)
result.add(new ArrayList<Integer>(element));
return;
}
for(int i=0; i<num.length; i++){
if(!visited[i]){
visited[i] = true;
element.add(num[i]);
helper(num, result, element, visited);
// After providing a complete permutation, pull out the last number,
element.remove(element.size()-1);
visited[i] = false;
}
}
}
}
=======================================
Update on 5th Feb 2014:
A more systematic for solving permutation problems, which is also to align with problem [leet code] N-Queens & II. Description can be referred to N-Queens problem.
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> rs = new ArrayList<ArrayList<Integer>>();
helper(num, 0, rs);
return rs;
}
public void helper(int[] num, int index, ArrayList<ArrayList<Integer>> rs){
if(index == num.length) {
ArrayList<Integer> singlePer = new ArrayList<Integer>();
for(int i=0; i<num.length; i++) singlePer.add(num[i]);
rs.add(singlePer);
}
else{
for(int i=index; i<num.length; i++){
int temp = num[index];
num[index] = num[i];
num[i] = temp;
helper(num,index+1,rs);
num[i] = num[index];
num[index] = temp;
}
}
}
}