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

    Better Sidewalks Could Bring Improved Public Health Most of our serious illnesses and deaths in the ...

  2. Python 连接Oracle数据库

    连接:python操作oracle数据库  python——连接Oracle数据库 python模块:cx_Oracle, DBUtil 大概步骤: 1. 下载模块 cx_Oracle (注意版本) ...

  3. 基于jQuery Ajax实现无刷新文件上传

    最近因项目需求更改,需要实现选择文件后即时上传至服务器,然后提交后,加载xls表格内容到jqgrid表格中,文件上传功能实现示例: 前端jsp页面: <form id="uploadF ...

  4. 【Lintcode】029.Interleaving String

    题目: Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2 ...

  5. Java远程调用

    一.  概念: RMI全称是Remote Method Invocation-远程方法调用,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网络分布式应用系统的核心解决方案之一.它支持 ...

  6. 推荐几个Laravel 后台管理系统

    小编推荐几个Laravel 后台管理系统 由百牛信息技术bainiu.ltd整理发布于博客园 一.不容错过的Laravel后台管理扩展包 —— Voyager 简介Voyager是一个你不容错过的La ...

  7. nagios客户端安装与配置windows篇

    一.被监控的windows xp客户端的配置 1.安装NSClient++并安装下载地址: http://sourceforge.net/projects/nscplusNSClient++-0.3. ...

  8. openstack 虚拟机添加网卡

    Openstack dashborad是没有给虚拟机添加网卡这个功能的,但是后台是有这行的接口的. 首先我们创建一个虚拟机,这个虚拟机制11.11.11网段的如图:

  9. Swing 添加Esc快捷键退出程序

    JFrame demo = new JFrame(); demo.addKeyListener( new KeyListener(){ public void keyReleased(KeyEvent ...

  10. Matcher的replaceAll ()/appendReplacement()/appendTail()详细举例

    直接上例子: package com.dajiangtai.djt_spider.util; import java.util.regex.Matcher;import java.util.regex ...