LeetCode——全排列
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
import java.util.ArrayList;
import java.util.List;
import java.util.Stack; public class FullSort { private List<List<Integer>> lists = new ArrayList<List<Integer>>();
public List<List<Integer>> permute(int[] nums) {
if(nums.length == 0) {
return lists;
}
else{
allSort(nums,new Stack<Integer>());
return lists;
}
}
//递归函数
public void allSort(int[]nums,Stack<Integer> stack)
{
//终止条件
if(stack.size() == nums.length )
{
lists.add(new ArrayList<Integer>(stack));
return;
}
for (int num : nums) {
if( stack.contains(num) ) {
continue;
}
stack.push(num);
allSort(nums,stack);
//出栈
stack.pop();
}
}
}
LeetCode——全排列的更多相关文章
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...
- Leetcode 全排列专题(更新ing)
总览 涉及到的题目有 题号 名字 难度 Leetcode 60 第k个排列 中等 Leetcode 46 全排列 中等 待更新...... Leetcode 46 全排列 题目 基础题 给定一个 没有 ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
随机推荐
- PLSQL报错: ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务
一开始吓尿了,以为自己动着什么东西了把数据库玩坏了,谁知道打开服务发现服务没启动........... 我们要确保数据库服务是启动状态
- 技术选型之Docker容器引擎
https://mp.weixin.qq.com/s?__biz=Mzg3NjAyOTUzMQ==&mid=2247484524&idx=1&sn=ac041bf3e36dda ...
- RN在Mac环境下开发环境搭建
1.推荐使用Homebrew来安装 Node 和 Watchman.在命令行中执行下列命令安装: brew install node brew install watchman 如果你已经安装了 No ...
- Sticky广播
sticky广播通过Context.sendStickyBroadcast()函数来发送,用此函数发送的广播会一直滞留,当有匹配此广播的广播接收器被注册后,该广播接收器就会收到此条信息. 使用此函数需 ...
- Selenium 2自动化测试实战27(unittest重要概念,test fixture、test case、test suite和test runne)
一.unittest重要概念 4个重要概念:test fixture.test case.test suite和test runner. 1.Test Case一个TestCase的实例就是一个测试用 ...
- go 基础 结构体 接口 访问权限
package School type SchoolModel struct { Name string Address string StudentCount int Is985 bool } ty ...
- makeObjectsPerformSelector的使用
NSArray 类定义的方法: makeObjectsPerformSelector:这是数组用的方法,类似于for循环. makeObjectsPerformSelector:@selector(m ...
- 【D3D12学习手记】The Swap Chain and Page Flipping
为了避免动画中的闪烁,最好将整个动画帧绘制到称为后台缓冲区的屏幕外纹理(off-screen texture)中.一旦整个场景被绘制到给定动画帧的后缓冲区,它就作为一个完整的帧呈现给屏幕;以这种方式, ...
- 【转】zookeeper之 zkServer.sh命令、zkCli.sh命令、四字命令
[FROM]https://www.cnblogs.com/andy6/p/7674028.html 一.zkServer.sh 1.查看 zkServer.sh 帮助信息 [root@bigdata ...
- .NET 实现复制粘贴功能
老是把自己当作珍珠,就时时有怕被埋没的痛苦.把自己当作泥土吧,让众人把你踩成一条道路. -----<泥土>鲁藜 .NET如何实现复制粘贴功能,具体代码如下: aspx文件: <div ...