LintCode——全排列
描述:给定一个数字列表,返回其所有可能的排列。
样例:给出一个列表[1,2,3],其全排列为:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
说明:分别使用递归和非递归实现
Java
1、递归
public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null)
return res;
if(nums.length == 0)
{
res.add(new ArrayList<Integer>());
return res;
}
ArrayList<Integer> list = new ArrayList<>();
dfs(res, list, nums);
return res;
}
private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums) {//深度优先
int n = nums.length;
if(list.size() == n)
{
res.add(new ArrayList<Integer>(list));
return;
}
for(int i = 0;i < n;i++) {
if(list.contains(nums[i]))
continue;
list.add(nums[i]);
dfs(res, list, nums);
list.remove(list.size() - 1);
}
}
}
2、非递归
public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums == null)
return res;
if( nums.length == 0){
res.add(new ArrayList<Integer>());
return res;
}
List<Integer> list = new ArrayList<>();
list.add(nums[0]);
res.add(new ArrayList<Integer>(list));
for(int i=1;i<nums.length;i++){
int size1 = res.size();
for(int j=0;j<size1;j++){
int size2 = res.get(0).size();
for(int k=0;k<=size2;k++){
ArrayList<Integer> temp = new ArrayList<>(res.get(0));
temp.add(k,nums[i]);
res.add(temp);
}
res.remove(0);
}
}
return res;
}
}
LintCode——全排列的更多相关文章
- [LintCode] 全排列
递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of permutati ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- lintcode 中等题:permutations II 重复数据的全排列
题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- lintcode Permutation Index
题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LintCode 190: Next Permutation
LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
随机推荐
- Spring Cloud 子项目介绍
Spring Cloud由以下子项目组成. Spring Cloud Config 配置中心——利用git来集中管理程序的配置. 项目地址:https://spring.io/projects/spr ...
- Hadoop HBase概念学习系列之HLog(二)
首先,明确,HRegion服务器包含两大部分:HLog和HRegion. HLog用来存储数据日志,采用的是先写日志的方式. 当用户需要更新数据的时候,数据会被分配到对应的HRegion服务器上提交修 ...
- python reload(sys)找不到,name 'reload' is not defined和Python3异常-AttributeError: module 'sys' has no att
基于python3.6.1版本,在一个.py文件中,加入这3行:import requests, re, sysreload(sys)sys.setdefaultencoding("utf- ...
- ATP学姐的模拟赛
ATPの水题大赛 声明:不是我觉得这题水,这就是本场模拟赛的名称. T1:求所有的$n$位数中有几个数满足:每一位要么是$A$要么是$B$,并且这个$n$位数的每一位加起来是$A$或$B$的倍数. $ ...
- 【洛谷】【前缀和+st表】P2629 好消息,坏消息
[题目描述:] uim在公司里面当秘书,现在有n条消息要告知老板.每条消息有一个好坏度,这会影响老板的心情.告知完一条消息后,老板的心情等于之前老板的心情加上这条消息的好坏度.最开始老板的心情是0,一 ...
- 可以获取JVM信息的一些管理工具类
一些可以获取JVM信息的java工具类 BufferPoolMXBean.class ClassLoadingMXBean.class CompilationMXBean.class GarbageC ...
- 记录一次elasticsearch-php工作过程
初始化 $hosts = array('192.168.30.41'); $this->client = \Elasticsearch\ClientBuilder::create()->s ...
- CSS 文本
CSS 文本属性可定义文本的外观. 通过文本属性,您可以改变文本的颜色.字符间距,对齐文本,装饰文本,对文本进行缩进,等等. 缩进文本 把 Web 页面上的段落的第一行缩进,这是一种最常用的文本格式化 ...
- springbatch入门练习(第一篇)
先搞懂几个概念 Job Respository: 作业仓库,负责Job.Step执行过程中的状态保存 Job launcher: 作业调度器,提供执行Job的入口 Job:作业,由多个step组成,封 ...
- odoo之recoed.append()方法
# 这里只是带数据到订单里面去,所以append要加append((0,0,)数据已经存在,放入到表中if dict: for line in dict: record.append((0,0,{'s ...