Java for LeetCode 046 Permutations
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
解题思路:
本题解题方法多多,为防止重复,决定使用Set的dfs算法,JVVA实现如下:
static public List<List<Integer>> permute(int[] nums) {
Set<List<Integer>> set=new HashSet<List<Integer>>();
dfs(set,nums,0);
return new ArrayList<List<Integer>>(set);
}
static List<Integer> list=new ArrayList<Integer>();
static public void dfs(Set<List<Integer>> set,int[] nums,int depth){
if(depth==nums.length){
set.add(new ArrayList<Integer>(list));
return;
}
for(int i=0;i<=depth;i++){
list.add(i,nums[depth]);
dfs(set,nums,depth+1);
list.remove(i);
}
}
Java for LeetCode 046 Permutations的更多相关文章
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 046 Permutations 全排列
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have th ...
- LeetCode 046 Permutations
题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
随机推荐
- 【前端】Sublime text3 插件LiveReload 实现实时预览
1.首先要安装插件LiveReload Sublime text3. 菜单 preferences->packages control,输入install.. 回车,输入LiveReload回车 ...
- 【CodeForces 577B】Modulo Sum
题 题意 给你n(1 ≤ n ≤ 106)个数a1..an(0 ≤ ai ≤ 109),再给你m( 2 ≤ m ≤ 103)如果n个数的子集的和可以被m整除,则输出YES,否则NO. 分析 分两种情况 ...
- Java中的继承、封装、多态、抽象
1.继承 java 和某些面向对象语言(如 c++)在实现继承的不同之处在于java只支持单继承,不支持多重继承.即java 中一个类只能继承于另一个类.我们将被继承的类称之为父类(基类),继承类称之 ...
- 网络包处理工具NetBee
What is NetBee? NetBee is a new library intended for several types of packet processing, such as pac ...
- Android 设计模式 之 观察者模式
/* * 观察者模式 * 定义对象间的一种一个(Subject)对多(Observer)的依赖关系,当一个对象的状态发送改变时,所以依赖于它的 * 对象都得到通知并被自动更新 * * 当然, ...
- 位图索引:原理(BitMap index)
http://www.cnblogs.com/LBSer/p/3322630.html 位图(BitMap)索引 前段时间听同事分享,偶尔讲起Oracle数据库的位图索引,顿时大感兴趣.说来惭愧,在这 ...
- Apache配置默认首页
操作系统:CentOS 6.5 Apache默认主页为index.html,如果要修改为index.php或其它,需要修改httpd.conf文件 用vim或其它编辑器打开httpd.conf 在上图 ...
- windows2003安全加固
因为IIS的方便性和易用性,使它成为最受欢迎的Web服务器软件之一.但是,IIS的安全性却一直令人担忧.如何利用IIS建立一个安全的Web服务器,是很多人关心的话题.要创建一个安全可靠的Web服务器, ...
- Song Jiang's rank list
Song Jiang's rank list Time Limit:1000MS Memory Limit:512000KB 64bit IO Format:%I64d & ...
- tolua.cast的实用方法
local name = (tolua.cast(sender, "ccui.Button")):getTitleText()