lintcode--全排列
来源:互联网 发布:js课程设计 编辑:程序博客网 时间:2024/04/27 17:26
给定一个数字列表,返回其所有可能的排列。
注意事项
你可以假设没有重复数字。
给出一个列表[1,2,3]
,其全排列为:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
]
//回溯法public class Solution { public List<List<Integer>> permute(int[] nums) { ArrayList<List<Integer>> lists = new ArrayList<>(); if (nums == null) return lists; if (nums.length == 0) { lists.add(new ArrayList<Integer>()); return lists; } ArrayList<Integer> list = new ArrayList<Integer>(); helper(lists, list, nums); return lists; } public void helper(ArrayList<List<Integer>> lists, ArrayList<Integer> list, int[] nums){ //如果已经全部排完 if(list.size() == nums.length) { lists.add(new ArrayList<Integer>(list)); return; } for(int i = 0; i < nums.length; i++){ if(list.contains(nums[i])){ continue; }else{ list.add(nums[i]); helper(lists, list, nums); list.remove(list.size() - 1); } //list.remove(list.size() - 1);这里也可以 } }}
- LintCode:全排列
- lintcode,字符串全排列
- lintcode,全排列
- LintCode 15 全排列
- lintcode--全排列
- LintCode-----15.全排列
- 全排列-LintCode
- LintCode permutations(全排列)
- lintcode--全排列
- Lintcode 15. 全排列
- LintCode-----16.带重复的全排列
- (lintcode)第15题 全排列(没有重复数字)
- LintCode 15-全排列 16-带重复元素的全排列
- LintCode-下一个排列
- lintcode-下一个排列
- lintcode-上一个排列
- LintCode 197. 排列序号
- lintcode ----排列序号
- 获取所有非manager的员工emp_no
- 教你如何计算海明码
- 使用flashback恢复数据
- [题解]bzoj3813 奇数国
- sql语句
- lintcode--全排列
- 无线信道信道衰落知识点总结
- java中一个字符串的大小写转换
- 【初学Linux】Web开发环境搭建——JDK的安装
- Docker-compose配置文件详解
- NYOJ289苹果背包系列
- 通过response生成验证码 java验证码
- 求最长一个串中的最长相同子串
- cloudera-manager内存阈值问题