LeetCode刷题笔录 Permutaions
来源:互联网 发布:u3软件 编辑:程序博客网 时间:2024/06/05 00:51
不用递归的解法自己想不出来,在这里
public class Solution { public ArrayList<ArrayList<Integer>> permute(int[] num) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); //start from an empty list result.add(new ArrayList<Integer>()); for (int i = 0; i < num.length; i++) { //list of list in current iteration of the array num ArrayList<ArrayList<Integer>> current = new ArrayList<ArrayList<Integer>>();for (ArrayList<Integer> l : result) { // # of locations to insert is largest index + 1 for (int j = 0; j < l.size()+1; j++) { // + add num[i] to different locations l.add(j, num[i]); ArrayList<Integer> temp = new ArrayList<Integer>(l); current.add(temp); // - remove num[i] add l.remove(j); } } result = new ArrayList<ArrayList<Integer>>(current); } return result; }}
0 0
- LeetCode刷题笔录 Permutaions
- LeetCode Permutaions
- LeetCode Permutaions II
- Leetcode刷题笔录 Anagrams
- LeetCode刷题笔录Combinations
- LeetCode刷题笔录Subsets
- LeetCode刷题笔录Triangle
- LeetCode刷题笔录LRUCache
- LeetCode刷题笔录 Two Sum
- LeetCode 刷题笔录 Add Two Numbers
- LeetCode刷题笔录 Reverse Integer
- LeetCode刷题笔录 Reorder List
- LeetCode刷题笔录 Jump Game II
- LeetCode刷题笔录 Search Insert Position
- LeetCode刷题笔录 Pow(x,n)
- LeetCode刷题笔录 Rotate List
- LeetCode刷题笔录 Single Number
- LeetCode刷题笔录 Same Tree
- SQL Syntax - more about SQL
- SQL中DateTime转换成Varchar样式
- Android界面——ImageView的一些事
- iOS- 给App添加内购& 验证购买iOS7新特性
- Wget用法、参数解释
- LeetCode刷题笔录 Permutaions
- RotatedRect 可选择矩形 的绘制
- 黑马程序员_Foundation框架02_NSString
- Populating Next Right Pointers in Each Node -- LeetCode
- 黑马程序员_Foundation框架04_NSNumber、NSValue、NSDate
- 统计二进制数中1的个数
- USB设备不能用。提示Windows 无法启动这个硬件设备。 (代码 19)
- 磁通和磁通链,电感关系
- 广告术语:CPM、CPC、CPA、PFP、CPS、CPL、CPR介绍