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 ...
随机推荐
- memcached高可用
http://sourceforge.net/projects/repcached/ memcached-1.2.8-repcached-2.2.tar.gz tar zxvf memcached-1 ...
- Father Christmas flymouse
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3479 Accep ...
- javacpp-FFmpeg系列补充:FFmpeg拉流截图实现在线演示demo(视频截图并返回base64图像,支持jpg/png/gif/bmp等多种格式)
javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...
- HDU1540(线段树统计连续长度)
---恢复内容开始--- Tunnel Warfare Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- ceph应用情况分析
1.概述 ceph是分布式的开源存储系统,同时支持块存储.对象存储和文件系统,ceph可以满足高性能.高可靠性和高扩展等特性. 目前ceph作为开源分布式存储已经被大量使用,尤其是在云环境下的应用,下 ...
- ceph 删除了osd但是osd目录保存完整如何恢复
1. 这里假设有一个osd被删除了 执行下列步骤删除: ceph osd out osd.0 service ceph stop osd.0 ceph osd crush remove osd.0 c ...
- 集成hibernateDaoSupport实现增删改查
1. package edu.jlu.fuliang.dao.impl; import java.util.List; import org.springframework.orm.hibernate ...
- 你所不知道的html5与html中的那些事(四)——文本标签
文章简介: 关于html5相信大家早已经耳熟能详,但是他真正的意义在具体的开发中会有什么作用呢?相对于html,他又有怎样的新的定义与新理念在里面呢?为什么一些专家认为html5完全完成后 ...
- 面试题: 数据库操作面试 已看1 很典型的sql面试题
摘要:今天参加了大展公司的一个电话面试,那位先生首先问我查询一个表的问题,条件是:1.一个数据表,有username字段.2.查询数据表中姓名姓张的.姓李的.姓刘的总数,并展现在一张表中.我当时就糊涂 ...
- Struts2+JQuery+Json登陆实例
Struts2+JQuery+Json登陆实例 博客分类: Struts2 在搭建之前.. 首先,需要准备struts2.0框架的5个核心包, 以及jsonplugin-0.32.jar 以及js ...