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. java核心技术记录之集合

    java库中的具体集合: 集合类型 描述 ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行高效地插入和删除操作的有序序列 ArrarDeque 一种用 ...

  2. 一天,python搞个分析NGINX日志的脚本

    准备给ZABBIX用的. 统计接口访问字次,平均响应时间,4XX,5XX次数 以后可以再改进.. #!/usr/bin/env python # coding: utf-8 ############# ...

  3. TortoiseSVN文件夹及文件图标不显示解决方法(转发)

    地址:http://blog.csdn.net/lishehe/article/details/8257545 由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能 ...

  4. Python 中的 TK编程

    可爱的 Python:Python 中的 TK编程 http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-12/ python che ...

  5. MIT算法导论——第四讲.Quicksort

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  6. Spring + JMS + ActiveMQ实现简单的消息队列(监听器异步实现)

    首先声明:以下内容均是在网上找别人的博客综合学习而成的,可能会发现某些代码与其他博主的相同,由于参考的文章比较多,这里对你们表示感谢,就不一一列举,如果有侵权的地方,请通知我,我可以把该文章删除. 1 ...

  7. Hibernate逍遥游记-第7章 Hibernate的检索策略和检索方式(<set lazy="false" fetch="join">、left join fetch、FetchMode.JOIN、)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  8. Delphi语言获得生命的原因和过程

    都说Anders Hejlsberg是Delphi语言的作者,前一阵仔细读了VCL源码,惊叹于它的巧夺天工,未免对编译器的作者有些不服气,觉得首功不是他.今天仔细想了想,还是觉得不服不行.以下是我的理 ...

  9. 最受欢迎的ASP.NET的CMS下载

    1. Umbraco 项目地址 下载 Umbraco是一个开放源码的CMS内容管理系统,基于asp.net建立,使用mssql进行存储数据.使用Umbraco,设计师能创造出有效的XHTML标记模板和 ...

  10. html 表单笔记

         表单 表单中主要包括下列元素: button——普通按钮radio ——单选按钮checkbox——复选框select ——下拉式菜单text ——单行文本框textarea——多行文本框s ...