Leetcode 46. Permutations
来源:互联网 发布:知乎b站三国演义 编辑:程序博客网 时间:2024/06/08 08:12
/** * e.g. nums = 1, 2, 3 * bt() * tmp(1) * bt() * tmp(1, 2) * bt() * tmp(1, 2, 3) * bt() * add(tmp) return * tmp.remove(2) * i == 2 end return * remove(1) * i == 1 tmp(1, 3) * bt() * tmp(1, 3, 2) * bt() * add(tmp) return * ... */ public class Solution { public static void backTrack(int[] nums, List<Integer> tmp, List<List<Integer>> res) { if (tmp.size() == nums.length) { res.add(new ArrayList<>(tmp)); return; } else { for (int i=0; i<nums.length; i++) { if (tmp.contains(nums[i])) continue; // time complexity for ArrayList.contains is O(n) tmp.add(nums[i]); backTrack(nums, tmp, res); tmp.remove(tmp.size()-1); } } } public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); backTrack(nums, new ArrayList<>(), res); return res; }}
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
- 布尔巴基的学术风格
- T-SQL 语句(一)—— 数据库操作
- Leetcode 213. House Robber II
- Leetcode 47. Permutations II
- 快速排序算法浅析
- Leetcode 46. Permutations
- Java_每天学习一点点之Java开发实战经典第三章习题
- 装饰者模式
- CKEditor+CKFinder服务器临时文件存储路径的问题
- 如何修改virtualBox下Ubuntu的虚拟内存
- 当jsp页面完全加载完成后执行一个js函数
- 339. Nested List Weight Sum
- 循环队列的实现(C语言版)
- eclipse之导入项目