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

 


posted @ 2019-01-31 13:03  Schwifty  阅读(165)  评论(0编辑  收藏  举报