046 Permutations 全排列
给定一个含有不同数字的集合,返回所有可能的全排列。
比如,
[1,2,3] 具有如下排列:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
详见:https://leetcode.com/problems/permutations/description/
Java实现:
参考:https://blog.csdn.net/jacky_chenjp/article/details/66477538
class Solution {
public List<List<Integer>> permute(int[] nums) {
// 最终返回的结果集
List<List<Integer>> res = new ArrayList<List<Integer>>();
int size = nums.length;
if (size==0||nums==null){
return res;
}
helper(nums, 0,res);
return res;
}
private void helper(int[] nums, int start, List<List<Integer>> res) {
// 将当前数组加到结果集中
if(start==nums.length) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<nums.length; i++){
list.add(nums[i]);
}
res.add(list);
return ;
}
// 将当前位置的数跟后面的数交换,并搜索解
for (int i=start; i<nums.length; ++i) {
swap(nums, i, start);
helper(nums, start+1, res);
swap(nums, i, start);
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
参考:http://www.cnblogs.com/grandyang/p/4358848.html
046 Permutations 全排列的更多相关文章
- LeetCode 046 Permutations 全排列
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have th ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- LeetCode 046 Permutations
题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
- [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 ...
- 【LeetCode】046. Permutations
题目: Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] ha ...
随机推荐
- 网络编程学习笔记-listen函数
listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定相应的套接字变为被动连接 ...
- ACM学习历程—HDU 5317 RGCDQ (数论)
Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...
- 开发工作之外的修炼Live笔记
“开发工作之外的修炼”这期Live分享了下列话题: [1] 如何发现自己的兴趣 [2] 财富.资源与被动收入 [3] 目标管理 [4] 快速做选择 [5] 时间管理 [6] 如何投资自己 >&g ...
- ContextMenu的自定义
1.针对整个ContextMenu, 自定义一个Style,去掉竖分割线 <Style x:Key="DataGridColumnsHeaderContextMenuSty ...
- POJ2387(最短路入门)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38556 Accepted ...
- 搭建基于Nagios的监控系统——之监控远程Windows服务器
分享了如何监控Linux服务器,我们来看看使用Nagios如何监控Windows服务器. 第一部分:配置被监控的Windows服务器 首先,访问 http://sourceforge.net/pr ...
- POJ-3669
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21055 Accepted: 5499 De ...
- IE各栏的截图说明
工具栏 包括 状态栏 命令栏 菜单栏 收藏栏 IE工具 > 工具栏 > 状态栏 有状态栏显示 无状态栏显示 菜单栏 快捷键 alt 可以快速展示 菜单栏 ,查看 ...
- Laravel框架接入短信平台进行用户注册短信验证
今天刚接触了一个短信接口平台,云通讯第三方短信提供服务商.http://www.yuntongxun.com/ 然后介绍一下怎么使用该短信平台来接入到自己的项目中. 首先你的去注册一个账号,然后根据提 ...
- nginx是如何处理一个请求的(包含https配置)
配置https首先要有ssl证书,这个证书目前阿里有免费的,但如果自己做实验,也是可以自签证书,只不过不受信 openssl genrsa -des3 -out server.key 1024 ...