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 2823 Sliding Window (线段树/单调队列)

    题目不说了,可以用线段树或者单调队列,下面附上代码. 线段树: #include <iostream> #include <stdio.h> #include <algo ...

  2. java基础知识回顾之java Thread类学习(三)--java线程实现常见的两种方式实现好处:

    总结:实现Runnable接口比继承Thread类更有优势: 1.因为java只能单继承,实现Runnable接口可以避免单继承的局限性 2.继承Thread类,多个线程不能处理或者共享同一个资源,但 ...

  3. 转一篇:文档笔记之Django QuerySet

    这个放着,说不定以后作一些更深入的查询时,用得着的. http://www.rapospectre.com/blog/7/ 今天刚刚答完辩体完检就跑来更新博客了!!! 先补上第一篇: 一般情况下,我们 ...

  4. lintcode 中等题:permutations 全排列

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

  5. lintcode: 爬楼梯

    题目: 爬楼梯 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,中不同的方法 返回 3 解题: 动态规划题目,同时还是有顺序 ...

  6. unity UGUI动态字体显示模糊

    设置Unity中ttf文件的Character为Unicode,点击apply

  7. 用React.addons.TestUtils、Jasmine进行单元测试

    一.用到的工具 1.React.addons.TestUtils 2.Jasmine 3.Browserify(处理jsx文件的require依赖关系) 4.Reactify(能和browserify ...

  8. NPOI技术,

    using(FileStream stream=new FileStream("C:\Users\XXXXXX\Desktop\1.xls",FileMode.Open))     ...

  9. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  10. 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验

    一,使用Struts2标签库简化表单 在文章[基于Struts2框架实现登录案例]的基础上,通过使用Struts标签库可以简化登录页面login2.jsp <%@ page language=& ...