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. HDU 2136 Largest prime factor(查找素数,筛选法)

    题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...

  2. python—命名规范(转)

    文件名全小写,可使用下划线 包应该是简短的.小写的名字.如果下划线可以改善可读性可以加入.如mypackage. 模块与包的规范同.如mymodule. 类总是使用首字母大写单词串.如MyClass. ...

  3. MQTT之 Mosquitto hello world的使用

    服务端发布消息模式,客户端订阅: 终端一中启动 moquitto 服务器 shallbeThatIshallbe:mosquitto iamthat$ 1427293344: Opening ipv4 ...

  4. python 利用smtp发送邮件,html格式

    def send_mail(to_list, sub, context):#sentmail to the maillist ''' to_list: 发送给谁 sub: 主题 context: 内容 ...

  5. 让Windows蓝屏死机

    ssdt 随便一个函数入口改90就蓝了 ------------------------------------------------- program Project2; uses Windows ...

  6. Quartz 并发/单线程

    Quartz 并发/单线程 Quartz定时任务默认都是并发执行的,不会等待上一次任务执行完毕,只要间隔时间到就会执行, 如果定时任执行太长,会长时间占用资源,导致其它任务堵塞.1.在Spring中这 ...

  7. List应用举例

    1.集合的嵌套遍历 学生类: package listexercise; /** * Created by gao on 15-12-9. */ public class Student { priv ...

  8. Spring Boot实现一个监听用户请求的拦截器

    项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...

  9. js判断浏览器类型 js判断ie6不执行

    js判断浏览器类型 $.browser  对象 $.browser.version 浏览器版本 var binfo = ''; if ($.browser.msie) { binfo = " ...

  10. 【HDOJ】4080 Stammering Aliens

    1. 题目描述给定一个长为$n \in [1, 4000]$的字符串,求其中长度最长的子串,并且该子串在原串中出现至少$m$次,并求最右起始位置. 2. 基本思路两种方法:二分+后缀数组,或者二分+哈 ...