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. makefile filter &&filter-out

    sources := foo.c bar.c baz.s abc.h    foo: $(sources)            gcc $(filter %.c %.s,$(sources)) -o ...

  2. Java——常用类(File)

    [File]    <1>java.io.File类代表系统文件名(路径和文件名).         ----注意:这里代表的只是文件名,而不是物理上的文件(硬盘上的数据),通过该类无法读 ...

  3. CopyOnWrite 个人理解以及应用

    缘由 最近在看<Redis 设计与实现>,看到Redis的执行bgsave生成dump.rdb是根据CopyOnWrite的 之前也不是很懂为啥要有CopyOnWrite这个东西 翻看文章 ...

  4. Spring Cloud Commons教程(三)忽略网络接口

    有时,忽略某些命名网络接口是有用的,因此可以将其从服务发现注册中排除(例如,在Docker容器中运行).可以设置正则表达式的列表,这将导致所需的网络接口被忽略.以下配置将忽略“docker0”接口和以 ...

  5. SQL报错:ORA-00911:无效的字符错误

    转载自:https://blog.csdn.net/huangyanlong/article/details/38096469 *)ORA-00911:无效的字符错误——由编译环境下一个小错误引起.S ...

  6. apache cgi 程序: End of script output before headers

    测试linux Apache cgi程序: #include <stdio.h> int main(){ printf("abc"); ; } 目录:/var/www/ ...

  7. 将String转化成Stream,将Stream转换成String, C# Stream 和 byte[] 之间的转换(文件流的应用)

    static void Main( string[] args ) { string str = "Testing 1-2-3"; //convert string 2 strea ...

  8. nginx + tomcat + memcached 做负载均衡及session同步

    1.nginx配置 # For more information on configuration, see: # * Official English Documentation: http://n ...

  9. day47—JavaScript事件基础应用

    转行学开发,代码100天——2018-05-02 1.事件对象 JavaScript中事件对象通常用定义变量ev或event表示.为了兼顾浏览器兼容问题,定义事件对象为 var oEvent = ev ...

  10. ASP.NET Core 上传微信永久视频素材

    话不多说直接上源码 请求实体  public class AddVideoRequest    {        /// <summary>        /// 文件流        / ...