Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

实现思想:

给定一个数组[1,2,3,4]
1、[1,[2,3,4]] ---------->[2,3,4]的子数组计算--->[2,[3,4]],[3,[2,4]]和[4[2,3]]
2、[2,[1,3,4]]
3、[3,[1,2,4]]
4、[4,[1,2,3]]

从上面可以看出,需要使递归的思想。

java代码实现如下:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List; public class Solution { public List<List<Integer>> permute(int[] num) {
List<List<Integer>> list = new ArrayList<>();
if(num.length == 1){ //当数据中只有一个元素时,只需要一种情况
List<Integer>lst = new ArrayList<>();
lst.add(num[0]);
list.add(lst);
}
else{
for(int i = 0 ; i < num.length ; i++){
int[] num1 = new int[num.length-1];
int j = 0;
int k = 0;
while(j < num.length){ //while语句是用来得到当前数组的子数组
if(j != i){
num1[k++] = num[j++];
}
else
j++;
} List<List<Integer>> list1 = permute(num1);
Iterator<List<Integer>> it = list1.iterator();
while(it.hasNext()){ //取出子数组得到集合中与当前元素进行组成
List<Integer>lst = new ArrayList<>();
lst.add(num[i]);
lst.addAll( it.next());
list.add(lst);
}
}
}
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] num = {1,2,3};
System.out.println(new Solution().permute(num));
} }

Permutations java实现的更多相关文章

  1. 46. Permutations (JAVA)

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

  2. 46. Permutations

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

  3. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  4. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  5. Java for LeetCode 047 Permutations II

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

  6. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  7. Java for LeetCode 046 Permutations

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

  8. Permutations II leetcode java

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  9. 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)

    解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...

随机推荐

  1. POJ 3641

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6044   Accepted: 24 ...

  2. jQuery从主页面存取控制 iframe 中的元素,参数及方法

    从主页面上获取iframe下的某个对象,或使用iframe下的方法,或是获取iframe下某个doc元素,要求兼容各类浏览器,不仅仅ie; $(function() { $('#abgne_ifram ...

  3. IE插件DebugBar如何安装及使用

    DebugBar插件是一款功能很强大的IE插件,用户可以从各个不同的角度剖析Web页面内部,包括页面 详细代码.CSS样式表.所有链接.所有图片代码.脚本信息等等,不管你是编程大虾还是IT新人都和适合 ...

  4. sudo: /etc/sudoers 的模式为 0551,应为 0440

    环境:Ubuntu 12.04.4 LTS 32bit 本想修改/etc/sudoers文件,取消sudo权限的密码.但是因为sudoers文件无‘w’(写)的权限,然后用命令加写权限的时候加错了,加 ...

  5. 如何在服务(Service)程序中显示对话框

    原文:http://www.vckbase.com/index.php/wv/94 服务程序(Service)一般是不能和用户进行交互的,所以他一般是不能显示窗口的.要和用户进行交互(如显示窗口),我 ...

  6. 应用程序加载外部字体文件(使用AddFontResource API函数指定字体)

    /* MSDN: Any application that adds or removes fonts from the system font table should notify other w ...

  7. 在PowerDesigner中设计物理模型3——视图、存储过程和函数

    原文:在PowerDesigner中设计物理模型3--视图.存储过程和函数 视图 在SQL Server中视图定义了一个SQL查询,一个查询中可以查询一个表也可以查询多个表,在PD中定义视图与在SQL ...

  8. KMP入门题目[不定期更新]

    HDU 1711 Number Sequence(模板题) #include <cstdio> ; ; int N, M; int textS[MAXN]; int tarS[MAXL]; ...

  9. Entity Framework学习 - 2.增删改查

    1.增加数据 PirateBayEntities db = new PirateBayEntities(); T_Tests test = new T_Tests(); test.Name = &qu ...

  10. 自定义View(2)canas绘制基本图形的示例

    效果 代码: void drawSample(Canvas canvas) { /* * 方法 说明 drawRect 绘制矩形 drawCircle 绘制圆形 drawOval 绘制椭圆 drawP ...