leetcode-46. Permutations
来源:互联网 发布:手机小夜灯软件 编辑:程序博客网 时间:2024/05/21 07:50
leetcode-46. Permutations
题目:
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]
]
之前做过类似的题目,但是和这题思路还是有很大差别的。
这一题的基本思路就是典型的回溯法。但是问题是怎么去查询下一层迭代。基本的方法就是每次加入不同的pos位置,然后如果List本身包含了这个pos的值则说明已经加入过了。
当然这是针对这题的,如果是有重复值的话就比较麻烦了。目前的思路和之前一题有类似的情况,就是先排序然后判断当前pos和pos-1是否一致来跳过相同的加入。这样的思路行不行。不过这个我没写过了。以后有时间需要实践一下。
public class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> ret = new ArrayList<List<Integer>>(); helper(ret,nums,new ArrayList<Integer>()); return ret; } private void helper(List<List<Integer>> ret, int[] nums, ArrayList<Integer> list){ if(list.size()==nums.length){ ret.add(new ArrayList<Integer>(list)); return; } for(int i = 0 ; i < nums.length ; i++){ if(list.contains(nums[i])) continue; list.add(nums[i]); helper(ret,nums,list); list.remove(list.size()-1); } }}
0 0
- [LeetCode]46.Permutations
- LeetCode 46.Permutations
- 46. Permutations Leetcode Python
- LeetCode --- 46. Permutations
- [Leetcode] 46. Permutations
- [leetcode] 46.Permutations
- LeetCode 46. Permutations DFS
- 46. Permutations LeetCode
- Leetcode 46. Permutations
- leetcode 46. Permutations
- LeetCode *** 46. Permutations
- LeetCode 46. Permutations
- leetcode 46. Permutations
- LeetCode - 46. Permutations
- 【leetcode】46. Permutations
- leetcode 46. Permutations
- [LeetCode] 46. Permutations
- leetcode 46. Permutations
- ContentType 内容类型
- windows核心编程-线程可警告状态以及APC队列
- 寻找自幂数
- 查找图元方法中遇见的MapX的Bug
- mybatis-generator扩展教程系列 -- 自定义配置参数修改DAO,Mapper文件后缀
- leetcode-46. Permutations
- Android爬坑-iconfont动态加载
- Maven为本地、测试、生产等不同的环境打包
- codeforces #382 题解 735ABCD
- hdoj 1848 Fibonacci again and again(组合博弈, sg函数)
- 个人记录-LeetCode 40. Combination Sum II
- 网页本地登陆注册(永久保存)
- NEFU 20 穿过街道
- MongoDB线上实践指南-基础篇之支撑百亿级访问(二)