zoukankan      html  css  js  c++  java
  • 046 Permutations 全排列

    给定一个含有不同数字的集合,返回所有可能的全排列。
    比如,
    [1,2,3] 具有如下排列:
    [
      [1,2,3],
      [1,3,2],
      [2,1,3],
      [2,3,1],
      [3,1,2],
      [3,2,1]
    ]

    详见:https://leetcode.com/problems/permutations/description/

    Java实现:

    参考:https://blog.csdn.net/jacky_chenjp/article/details/66477538

    class Solution {
        public List<List<Integer>> permute(int[] nums) {  
            // 最终返回的结果集
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            int size = nums.length;
            if (size==0||nums==null){
                return res;
            }
            helper(nums, 0,res);
            return res;
        }
    
        private void helper(int[] nums, int start, List<List<Integer>> res) {
            // 将当前数组加到结果集中
            if(start==nums.length) {
                List<Integer> list = new ArrayList<>();
                for (int i=0; i<nums.length; i++){
                    list.add(nums[i]);
                }
                res.add(list);
                return ;
            }
            // 将当前位置的数跟后面的数交换,并搜索解
            for (int i=start; i<nums.length; ++i) {
                swap(nums, i, start);
                helper(nums, start+1, res);
                swap(nums, i, start);
            }
        }
    
        private void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }
    

    参考:http://www.cnblogs.com/grandyang/p/4358848.html

  • 相关阅读:
    js禁止空格的输入
    js对cookie的读写操作
    js实现读秒
    formData的简单使用
    图形验证码笔记
    Redis安装
    ZooKeeper安装
    OpenJDK 编译-Linux环境
    Java环境变量-Linux环境
    ArchLinux+Win10双系统的Grub配置
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8691188.html
Copyright © 2011-2022 走看看