给定一个含有不同数字的集合,返回所有可能的全排列。
比如,
[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

046 Permutations 全排列的更多相关文章

  1. LeetCode 046 Permutations 全排列

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

  2. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  3. LeetCode 046 Permutations

    题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...

  4. [LeetCode] Permutations 全排列

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

  5. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  6. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

  7. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. 46. Permutations (全排列)

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

  9. 【LeetCode】046. Permutations

    题目: Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] ha ...

随机推荐

  1. Python 微信通知 先挖个坑

    桑心病狂,试试把报警信息发到微信上 原文  https://segmentfault.com/a/1190000009717078  

  2. 为什么修改头文件make不重新编译

    make是根据依赖文件的时间戳来决定要不要重新编译的.在: object: deplist # actions 中,可以把头文件加进deplist,这样修改头文件后,make就会重新编译了. 单纯地修 ...

  3. ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  4. asm 兼容性、asm 主要参数管理

    一 ASM instance 与 Database instance 的版本兼容性说明 1. Oracle 11gR2 的ASM 支持11g和10g的数据库实例.但是在Oracle Clusterwa ...

  5. poj2492A Bug's Life——带权并查集

    题目:http://poj.org/problem?id=2492 所有元素加入同一个并查集中,通过其偏移量%2将其分类为同性与异性,据此判断事件. 代码如下: #include<iostrea ...

  6. PowerShell 总结

    PowerShell 总结 1. 2. 3. 参考学习资料 (1). PowerShell 在线教程 (2). 利用Powershell在IIS上自动化部署网站 视频教程: (3). Windows ...

  7. SpringMVC之七:SpringMVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  8. Haproxy 安装配置详解-端口映射

    Haproxy 简介: HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy特别适用于那些负载特大的web站点, ...

  9. Java集合框架(1)

    Collection接口:它是Java集合框架的一个根接口,也是List.Set和Queue接口的父接口.同时它定义了可用于操作List.Set和Queue的方法—增删改查. Map接口:它提供了一种 ...

  10. ambari2.1.1安装

    1       安装环境 系统:centos6.6 Ambari版本:2.1.1 安装指南:https://cwiki.apache.org/confluence/display/AMBARI/Ins ...