LeetCode 46 Permutations(全排列问题)
package leetcode_50; import java.util.ArrayList;
import java.util.List; /***
*
* @author pengfei_zheng
* 给定数组元素均不相同,求出所有全排列
*/
public class Solution46 {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
prem(list,new ArrayList<>(),nums,new boolean[nums.length]);
return list;
} private static void prem(List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used) {
if(tempList.size()==nums.length){
list.add(new ArrayList<>(tempList));
}
else{
for(int i = 0; i < nums.length; i++){
if(used[i] || i>0 && nums[i]==nums[i-1] && !used[i-1]) continue;
used[i]=true;
tempList.add(nums[i]);
prem(list,tempList,nums,used);
used[i]=false;
tempList.remove(tempList.size()-1);
}
}
}
public static void main(String[]args){
int []nums={1,2,3};
List<List<Integer>> list = permute(nums);
for(List<Integer> item:list){
System.out.println(item);
}
}
}
LeetCode 46 Permutations(全排列问题)的更多相关文章
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- 【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...
- [leetcode]47. Permutations全排列(给定序列有重复元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 46. Permutations (全排列)
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- 46 Permutations(全排列Medium)
题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...
- LeetCode 046 Permutations 全排列
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have th ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- linux中crontab命令
一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...
- Nagios监控mongodb分片集群服务实战
1,监控插件下载 Mongodb插件下载地址为:git clone git://github.com/mzupan/nagios-plugin-mongodb.git,刚開始本人这里没有安装gitpu ...
- 前端页面——Cookie与Session有什么差别
我们在实际生活中总会遇到这种事情,我们一旦登录(首次输入username和password)某个站点之后,当我们再次訪问的时候(仅仅要不关闭浏览器),无需再次登录.而当我们在这个站点浏览一段时间后,它 ...
- .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录?
.net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录? 使用VS2010开发应用程序完毕后,在发布应用程序时,常 ...
- UNIX环境编程学习笔记(5)——文件I/O之fcntl函数访问已打开文件的性质
lienhua342014-08-29 fcntl 函数可以改变已打开的文件的性质. #include <fcntl.h> int fcntl(int filedes, int cmd, ...
- input checkbox复选框点击获取当前选中状态jquery
function checkAll(id) { //用is判断 // let checkStatus=$(id).is(':checked'); // console.log(checkStatus) ...
- RancherOS(ROS)如何安装到硬盘? 并设置为用户自动登录到系统? -a rancher.autologin=tty1
RancherOS 安装到硬盘,一般都是通过ssh_authorized_keys 方式. ------------------------------------------- 从第一次认识到这个方 ...
- mysql中如何开启binlog?开启二进制日志文件?binary log?
需求描述: 开启mysql的binlog即binary log日志功能,在此记录下. 版本描述: mysql版本:5.7.21-log 操作过程: 1.修改my.cnf并且将以下参数加入其中,重启my ...
- Android图片处理(Matrix,ColorMatrix) - 转载
Android图片处理(Matrix,ColorMatrix) 转载自:http://www.cnblogs.com/leon19870907/articles/1978065.html 在编程中有时 ...
- SpringMVC由浅入深day02_10拦截器
10 拦截器 Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理. 10.1 拦截定义 定义拦截器,实现HandlerInte ...