zoukankan      html  css  js  c++  java
  • 46. Permutations

    Given a collection of distinct integers, return all possible permutations.

    Example:

    Input: [1,2,3]
    Output:
    [
      [1,2,3],
      [1,3,2],
      [2,1,3],
      [2,3,1],
      [3,1,2],
      [3,2,1]
    ]
    public class Solution {
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            if(nums == null || nums.length == 0){
                return res;
            }
            boolean [] used = new boolean[nums.length];  //By default,boolean数组的值是false。记住。
            helper(nums,used,new ArrayList<Integer>(), res);
            return res;
        }
        private void helper(int[] nums, boolean [] used, List<Integer> item, List<List<Integer>> res){
            if(item.size() == nums.length){
                res.add(new ArrayList<Integer>(item));  //细节,list是refer by reference,所以如果直接add(item)的话,item会一直瞎几把变
                return;
            }
            for(int i = 0; i<nums.length; i++){
                if(!used[i]){
                    used[i] = true;
                    item.add(nums[i]);
                    helper(nums,used,item,res);
                    item.remove(item.size()-1);//代表以nums[i]已经用完,退回
                    used[i] = false;//删了nums【i】used刷新
                }
            }
        }
    }

    随后我看到这篇文章,有点似懂非懂。

    https://www.cnblogs.com/lzxin/p/9714133.html

    import java.util.ArrayList;
    import java.util.List;
    
    public class Permutations {
    
        //题目描述:Given a collection of distinct integers, return all possible permutations.(给定一组不同的整数,返回其所有的可能组合)
        public List<List<Integer>> permute(int[] nums) {
            //一个全局变量,用于保存所有集合
            List<List<Integer>> list = new ArrayList<>();
            //传入三个参数,没有附加参数
            backtrack(list, new ArrayList<>(), nums);
            return list;
        }
    
        private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
            //一个终结条件,也就是满足条件的时候
            if(tempList.size() == nums.length){
                //全局变量添加一个满足条件的集合
                list.add(new ArrayList<>(tempList));
            } else{
                for(int i = 0; i < nums.length; i++){
                    if(tempList.contains(nums[i])) continue;
                    //如果tempList没有包含nums[i]才添加
                    tempList.add(nums[i]);
                    //递归调用,此时的tempList一直在变化,直到满足终结条件才结束
                    backtrack(list, tempList, nums);
                    System.out.println("tempList的内容:"+tempList+"-------"+"i的值:"+i);
                    //它移除tempList最后一个元素的作用就是返回上一次调用时的数据,也就是希望返回之前的节点再去重新搜索满足条件。这样才能实现回溯
                    tempList.remove(tempList.size() - 1);
                }
            }
        }
    
        public static void main(String[] args){
            int[] nums={1,2,3};
            (new Permutations()).permute(nums);
        }
    }

    main方法测试,输出语句的结果显示如下,可以观察出回溯的过程

    tempList的内容:[1, 2, 3]-------i的值:2
    tempList的内容:[1, 2]-------i的值:1
    tempList的内容:[1, 3, 2]-------i的值:1
    tempList的内容:[1, 3]-------i的值:2
    tempList的内容:[1]-------i的值:0
    tempList的内容:[2, 1, 3]-------i的值:2
    tempList的内容:[2, 1]-------i的值:0
    tempList的内容:[2, 3, 1]-------i的值:0
    tempList的内容:[2, 3]-------i的值:2
    tempList的内容:[2]-------i的值:1
    tempList的内容:[3, 1, 2]-------i的值:1
    tempList的内容:[3, 1]-------i的值:0
    tempList的内容:[3, 2, 1]-------i的值:0
    tempList的内容:[3, 2]-------i的值:1
    tempList的内容:[3]-------i的值:2

    
    
  • 相关阅读:
    Linux 安装apache2时候遇到的问题
    ubuntu安装php时出现error: xml2config not found. Please check your libxml2 installation 的解决办法
    Linux一块网卡绑定2个IP地址
    php 满足条件后跳转的几种方法.
    Linux 安装php 遇到 libtool: link: `ext/date/php_date.lo' is not a valid libtool object
    js 光标移动到输入框最后位置函数
    Linux iptables 防火墙相关命令介绍及使用
    tcpdump的简单选项介绍
    子网掩码转换函数
    Linux 十六进制转换十进制的函数
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10341334.html
Copyright © 2011-2022 走看看