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

  • 相关阅读:
    移动端meta标签
    document.ready 和 window.onload
    axios 源码分析
    vue 中的 el
    安卓和Ios 手机兼容性
    一些移动端问题
    Python 局部变量与全局变量
    Linux常用命令大全(非常全!!!)
    Python_爬虫_基础
    linux 常用命令
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8691188.html
Copyright © 2011-2022 走看看