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. Task4.用PyTorch实现多层网络

    1.引入模块,读取数据  2.构建计算图(构建网络模型) 3.损失函数与优化器 4.开始训练模型 5.对训练的模型预测结果进行评估 import torch.nn.functional as F im ...

  2. 关于Ubuntu 14.04 安装Oracle 11gR2安装步骤(从开始到放弃--最终使用docker获取)

    最近在复习Oracle的相关内容,好准备在下一份工作中能够熟练一些,所以准备在自己虚拟机中安装Oracle的11gR2版本,主要参考内容为: https://blog.csdn.net/qq_4025 ...

  3. linux运维、架构之路-CentOS6.9安装Zabbix3.4.1

    一.LAMP环境安装 1.环境 [root@m01 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@m01 ~]# uname ...

  4. Rosetta Stone 不在C盘安装步骤

    本文出自:http://www.cnblogs.com/2186009311CFF/p/7500637.html Rosetta Stone默认安装在C盘的,很不好,故找到次解决方案: 总体就是移动文 ...

  5. php array_shift()函数 语法

    php array_shift()函数 语法 作用:删除数组中第一个元素,并返回被删除元素的值.富瑞联华 语法:array_shift(array) 参数: 参数 描述 array 必需.规定数组. ...

  6. BZOJ 4026: dC Loves Number Theory 可持久化线段树 + 欧拉函数 + 数学

    Code: #include <bits/stdc++.h> #define ll long long #define maxn 50207 #define setIO(s) freope ...

  7. Android ListView焦点事件冲突问题与解决

    Android ListView对于单纯列表展示是很好用的,但是一旦牵扯到对listView进行操作就会遇到各种各样的问题.比如保存Checkbox状态与item复用的冲突.遇到可获取焦点的控件比如说 ...

  8. TestingWhiz社区版2013版下载地址

    TestingWhiz社区版 https://sourceforge.net/projects/testingwhiz-community-edition/ https://sourceforge.n ...

  9. 服务器中常见的RAID

    Standalone 最普遍的单磁盘储存方式. Cluster 集群储存是通过将数据分布到集群中各节点的存储方式,提供单一的使用接口与界面,使用户可以方便地对所有数据进行统一使用与管理. Hot sw ...

  10. 利用python进行数据分析--numpy基础

    随书练习,第四章  NumPy基础:数组和矢量运算 # coding: utf-8 # In[1]: # 加注释的三个方法1.用一对"""括起来要注释的代码块. # 2. ...