Permutations java实现
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实现的更多相关文章
- 46. Permutations (JAVA)
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- 46. Permutations
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- Java for LeetCode 046 Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...
- Permutations II leetcode java
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...
随机推荐
- POJ1068Parencodings
http://poj.org/problem?id=1068 这个题的话就是先把给出来的一串数字转化成括号,再把括号转化成要求的,最后输出就行了 #include<cstdio> #inc ...
- [SQL Server系] -- 约束
什么是约束? 约束(Constraint)是SQL Server中提供的 自动保存数据库完整性 的一种方法,定义了可输入表或表的列中的数据限制条件. SQL Server中共有5中约束 PRIMARY ...
- C# get set方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 防止注入网上查了下用SqlParameter可以,那SqlParameter处理单引号时候是自动转义了吗
String sql = String.Format(); SqlCommand com = new SqlCommand(sql, con); 一般而言,把 一个单引号,替换成 两个单引号就可以了.
- Python Snippet
python按行读取文件,如何去掉换行符"\n" for line in file.readlines(): line=line.strip('\n') python没有subst ...
- 【nginx网站性能优化篇(3)】反向代理实现负载均衡
注意,本篇文章为负载均衡的理论篇,后续找个机会推出实战篇.理论篇主要讲述如何配置负载均衡,配置负载均衡不难.难的是真正的实战,比如如何做到多服务器之间的数据共享(session,file等),多cac ...
- 【mongoDB中级篇②】索引与expain
索引的操作 数据库百分之八十的工作基本上都是查询,而索引能帮我们更快的查询到想要的数据.但是其降低了数据的写入速度,所以要权衡常用的查询字段,不必在太多字段上建立索引. 在mongoDB中默认是用bt ...
- AutoCAD图形打印出图片 C#
这几天搞cad二次开发,用的是C#语言,目前在网上找到的资料比较少.弄了两天,才做出怎样实现打印出图片.首先得在AutoCAD软件界面下,设置打印机的页面设置和打印机设备名称一样(以防打印不出来).即 ...
- Collection_Other
package com.bjsxt.others.que; import java.util.ArrayDeque; import java.util.Queue; /** * 使用队列模拟银行存款业 ...
- ListView(2)最简单的上拉刷新,下拉刷新
最简单的上拉刷新和下拉刷新,当listview滚动到底部时向上拉刷新数据.当listview滚动到最顶部时下拉刷新. 图1,上拉刷新 图2,下拉刷新 1,设置lisview,加载heade ...