46.Permutations (Medium)](https://leetcode.com/problems/permutations/description/)

[1,2,3] have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

题目描述:

  给定一个数组,返回数组中元素所能组成的所有序列。

思路分析:

  排列问题,可以用回溯的思路进行解决

代码:

public List<List<Integer>>permute(int []nums){
List<List<Integer>>res=new ArrayList<>();
if(nums==null||nums.length=0)
return res;
List<Integer>list=new ArrayList<>();
boolean []visited=new boolean[nums.length];
backtracking(nums,visited,res,list);
return res;
}
public void backtracking(int[]nums,boolean []visited,List<List<Integer>>res,List<Integer>list){
if(list.size()==nums.length){
res.add(new ArrayList<>(list));//重新构造一个List
return;
}
for(int i=0;i<nums.length;i++){
if(visited[i]==true)
continue;
visited[i]=true;
list.add(nums[i]);
backtracking(nums,visited,res,list);
list.remove(list.size()-1);
visited[i]=false;
}
}

回溯--- Permutations的更多相关文章

  1. 回溯---Permutations II

    47.Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) [1,1,2] hav ...

  2. 46. Permutations 回溯算法

    https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...

  3. Permutations(排列问题,DFS回溯)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  5. Leetcode之回溯法专题-46. 全排列(Permutations)

    Leetcode之回溯法专题-46. 全排列(Permutations) 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3, ...

  6. (待解决,效率低下)47. Permutations II C++回溯法

    思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果 但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的v ...

  7. 46. Permutations C++回溯法

    基本的回溯法 注意每次回溯回来要把上次push_back()进去的数字pop掉! class Solution { public: void backTrack(vector<int> n ...

  8. LeetCode题解 Permutations II 和 Permutations I ——回溯算法

    这个算法感觉还是很陌生的.算法导论里没有讲这个算法,而数据结构与算法分析只用了一节来阐述.我居然跳过去了..尴尬. 笨方法解决的: 第一题: 给定一个元素不重复的数组,枚举出他们的全排列. 方法1:递 ...

  9. Permutations 全排列 回溯

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

随机推荐

  1. jquery-时间轴滑动

    效果预览图: html: <div class="tim"> <div class="timdiv"> <div class=&q ...

  2. vue props父组件与子组件传值方法

    /~~父组件 runshow.vue~~/ <template> <div> <conditions :fenxiConditonsList="propCond ...

  3. [window] 使用Pyhton轻便好用的spyder IDE进行代码分析时如何指定相关的配置文件

    spyder 使用pylint这个第三方库进行代码检查,其实pylint使用的代码规范默认也是pep8,不过该库还有 其它用途,在这里我专门写写在代码分析时,如何指定配置文件 一般来说,使用spyde ...

  4. Dmango cxrf 自定义分页 缓存 session 序列化 信号量 知识点

    参考https://www.cnblogs.com/wupeiqi/articles/5246483.html

  5. 解决:父类中的@NotNull无效以及@Notnull 验证list对象无效

    解决方法如图: controller层 vo.param层 父类验证注解要使用@NotEmpty 不能使用 @NotNull,否则验证无效的,反正笔者是没有成功过

  6. 图像bayer格式介绍【转】

    本文转载自:http://www.cnblogs.com/whw19818/p/6223143.html 1 图像bayer格式介绍 bayer格式图片是伊士曼·柯达公司科学家Bryce Bayer发 ...

  7. Maven的一些常用命令

    将本项目的源码部署到本地仓库 mvn clean source:jar install 将本地jar包部署到本地仓库,首先将jar包放在当前目录下,然后执行,这样做比直接把jar包copy到本地仓库更 ...

  8. VMware 虚拟化编程(2) — 虚拟磁盘文件类型详解

    目录 目录 前文列表 虚拟磁盘文件 VMDK 用户可以创建的虚拟磁盘类型 VixDiskLib 中支持的虚拟磁盘类型 虚拟机文件类型 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/ ...

  9. Mysql新增字段到大数据表导致锁表

    昨天晚上7点左右,对一张表进行加字段,大概200多万条记录,字段90多个的大表,结果造成mysql锁表,进而导致服务不可用.执行语句如下: ALTER TABLE `sc_stockout_order ...

  10. IDEA基本设置和快捷键大全

    # IDEA基本设置 ## 设置编码格式 1. Configure - Settings - Editor - File Encodings 2. 将三个编码全部设置为UTF-8 ## 启用Ctrl+ ...