[leet code] Permutations

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值