LeetCode-46:Permutations
来源:互联网 发布:淘宝认证复核在哪里 编辑:程序博客网 时间:2024/05/16 13:44
原题描述如下:
Given a collection of distinct 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], [3,2,1]]
题意
:给定一个数字,求1到n的全排列
解题思路:
Java代码:
public class Solution {public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> resList = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
for(int i=0; i<nums.length; ++i){
list.add(nums[i]);
}
resList.add(list);
getRes(resList, 0, nums.length);
return resList;
}
public static void getRes(List<List<Integer>> resList, int k, int n){
if(k != n){
int size = resList.size();
for(int i=0; i<size; ++i){
List<Integer> list = resList.get(i);
for(int j=k+1; j<n; ++j){
List<Integer> templist = new ArrayList<Integer>(list);
int k1 = templist.get(k);
templist.set(k, templist.get(j));
templist.set(j, k1);
resList.add(templist);
}
}
getRes(resList, k+1, n);
}
}
}
0 0
- LeetCode 46 Permutations + LeetCode 47 Permutations II
- LeetCode 46: Permutations
- leetcode 46 Permutations
- LeetCode 46 Permutations II
- LeetCode(46) Permutations
- [leetcode 46] Permutations
- Leetcode NO.46 Permutations
- leetcode || 46、Permutations
- leetcode-46 Permutations
- leetcode 46 : Permutations
- Leetcode #46 Permutations
- LeetCode(46) Permutations
- leetcode 46: Permutations
- [leetcode-46]Permutations(java)
- LeetCode(46)Permutations
- [Leetcode]#46 Permutations
- leetcode 46:Permutations
- 【leetcode】【46】Permutations
- java练习+奇偶个数
- Linux学习——守护进程(daemon)
- Android:学习AIDL,这一篇文章就够了(下)
- 关于垃圾回收GC的整理
- 如何使用pt-duplicate-key-checker检查冗余索引
- LeetCode-46:Permutations
- const与指针
- 自然语言处理入门心得——书籍、课程推荐
- OC中自定义类的NSCopying实现的注意事项(isEqual & hash实现)
- 1-UNIX System Overview
- 设计模式之观察者模式
- Java并发编程:Callable、Future和FutureTask
- 海量数据处理 - 10亿个数中找出最大的10000个数(top K问题)
- 自学编程这几点非常重要