zoukankan      html  css  js  c++  java
  • lintcode15- Permutations- medium

    Given a list of numbers, return all possible permutations.

     Notice

    You can assume that there is no duplicate numbers in the list.

    Example

    For nums = [1,2,3], the permutations are:

    [
      [1,2,3],
      [1,3,2],
      [2,1,3],
      [2,3,1],
      [3,1,2],
      [3,2,1]
    ] 

    Challenge

    Do it without recursion.


    1. recursion 版本:

    和subset类似的递归。三参数current, nums, result。for循环内,满了deepcopy,没满试着加还没有的元素。

    subset答案是2n个,permutation的答案是n!个。

    所以subset是每次还没填满,按顺序试着加进去后就deepcopy一份。

    permutation是每次试着加加看,不断加入还没有的数字,满了再deepcopy一份。通过每一位上面都用for循环跑满来保证不同的顺序。

    2.非recursion版本

    从队伍排列里面获得启发,也就是每次把一个新的元素插入到已有队列里获得新排列嘛。

    三重循环。(新元素(当前已有的各种排列(插到的每个位置)))。

    因为每次有一变多的过程,所以记得deepcopy。

    3.思路跟2比较像的recursion版本:

    直接递归本函数。每次把nums后面n-1位的作为新数组去调用自身函数,得到的list里在各个位置插入第一个数nums[0]就得到了这层的排列了。


    1.

    public class Solution {
        /*
         * @param nums: A list of integers.
         * @return: A list of permutations.
         */
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
    
            if (nums == null){
                return null;
            }
    
            if (nums.length == 0){
                result.add(new ArrayList<Integer>());
                return result;
            }
    
            helper(new ArrayList<Integer>(), nums, result);
            return result;
        }
    
    
        private void helper(List<Integer> current, int[] nums, List<List<Integer>> result){
            if (current.size() == nums.length){
                result.add(new ArrayList<Integer>(current));
            }
    
            for (int i = 0; i < nums.length; ++i){
                if (current.contains(nums[i])){
                    continue;
                }
                current.add(nums[i]);
                helper(current, nums, result);
                current.remove(current.size() - 1);
            }
        }
    }

    2.

    public class Solution {
        /*
         * @param nums: A list of integers.
         * @return: A list of permutations.
         */
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            result.add(new ArrayList<Integer>());
    
            if (nums == null){
                return null;
            }
    
            if (nums.length == 0){
                return result;
            }
    
            for (int i = 0; i < nums.length; ++i){
                //出现了一变多,所以肯定要deepcopy的。
                List<List<Integer>> newResult = new ArrayList<List<Integer>>();
    
                for (List<Integer> permutation : result){
                    for (int idx = 0; idx <= permutation.size(); ++idx){
                        List<Integer> newPermutation = new ArrayList<Integer>(permutation);
                        newPermutation.add(idx, nums[i]);
                        newResult.add(newPermutation);
                    }
                }
    
                result = newResult;
            }
    
            return result;
        }
    }

    3.

    public class Solution {
        /*
         * @param nums: A list of integers.
         * @return: A list of permutations.
         */
        public List<List<Integer>> permute(int[] nums) {
            // write your code here
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            if (nums == null) {
                return result;
            }
            if (nums.length == 0) {
                result.add(new ArrayList<Integer>());
                return result;
            }
            if (nums.length == 1) {
                List<Integer> list = new ArrayList<Integer>();
                list.add(nums[0]);
                result.add(list);
                return result;
            }
            
            int[] subNums = new int[nums.length - 1];
            for (int i = 1; i < nums.length; i++) {
                subNums[i - 1] = nums[i];
            }
            List<List<Integer>> subPermutes = permute(subNums);
            for (int i = 0; i < nums.length; i++) {
                for (List<Integer> subPermute : subPermutes) {
                    List<Integer> newPermute = new ArrayList<Integer>(subPermute);
                    newPermute.add(i, nums[0]);
                    result.add(newPermute);
                }
            }
            return result;
        }
        
        
    }
  • 相关阅读:
    成都的收藏品市场
    微信小程序 如何定义全局函数?
    Linux下 安装VMware Tools工具
    小程序圆角进度条实现方法
    Excel 将换行符替换为空
    再次学习mysql优化
    Subl 命令
    时间见证着—eternal life
    大巧不工web前端设计修炼之道—笔记
    批量更新某字段内容
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7566602.html
Copyright © 2011-2022 走看看