排列组合
来源:互联网 发布:java线程与进程的 编辑:程序博客网 时间:2024/06/08 05:34
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
和N-Queens都是NP问题,利用helper迭代,迭代的stop condition是item.size() == k, 此时把item的copy加到res中。
若item.size()还没有到k, item加i, 然后继续迭代,到了k返回后去掉item最后一个值。
这里的start是递归的关键,每次recursion时, 加完i后,把i+1赋给start继续recursion.
*/
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> rst = new ArrayList<List<Integer>>();
List<Integer> solution = new ArrayList<Integer>();
helper(rst, solution, n, k, 1);
return rst;
}
private void helper(List<List<Integer>> rst, List<Integer> solution, int n, int k, int start) {
if (solution.size() == k){
rst.add(new ArrayList(solution));
//return;
}
for(int i = start; i <= n; i++){
solution.add(i);
// the new start should be after the next number after i
helper(rst, solution, n, k, i+1);
solution.remove(solution.size() - 1);
}
}
}
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]]public class Solution {
public List<List<Integer>> permute(int[] nums) {
ArrayList<List<Integer>> rst = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0) {
return rst;
}
ArrayList<Integer> list = new ArrayList<Integer>();
helper(rst, list, nums);
return rst;
}
public void helper(ArrayList<List<Integer>> rst, ArrayList<Integer> list, int[] nums){
if(list.size() == nums.length) {
rst.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(rst, list, nums);
list.remove(list.size() - 1);
}
}
}
Permutations II
DescriptionSubmissionsSolutions
Really easy Java solution, much easier than the solutions with very high vote
Use an extra boolean array " boolean[] used" to indicate whether the value is added to list.
Sort the array "int[] nums" to make sure we can skip the same value.
when a number has the same value with its previous, we can use this number only if his previous is used
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(nums==null || nums.length==0) return res; boolean[] used = new boolean[nums.length]; List<Integer> list = new ArrayList<Integer>(); Arrays.sort(nums); dfs(nums, used, list, res); return res; } public void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> res){ if(list.size()==nums.length){ res.add(new ArrayList<Integer>(list)); return; } for(int i=0;i<nums.length;i++){ if(used[i]) continue; if(i>0 &&nums[i-1]==nums[i] && !used[i-1]) continue; used[i]=true; list.add(nums[i]); dfs(nums,used,list,res); used[i]=false; list.remove(list.size()-1); } }}
DescriptionSubmissionsSolutions
Share my Java code with detailed explanantion
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); if (nums==null || nums.length==0) { return ans; } permute(ans, nums, 0); return ans; } private void permute(List<List<Integer>> ans, int[] nums, int index) { if (index == nums.length) { List<Integer> temp = new ArrayList<>(); for (int num: nums) { temp.add(num); } ans.add(temp); return; } Set<Integer> appeared = new HashSet<>(); for (int i=index; i<nums.length; ++i) { if (appeared.add(nums[i])) { swap(nums, index, i); permute(ans, nums, index+1); swap(nums, index, i); } } } private void swap(int[] nums, int i, int j) { int save = nums[i]; nums[i] = nums[j]; nums[j] = save; }}
More...
0 0
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- Java线程的基本运用
- 查询sql语句执行消耗的时间
- bzoj1014: [JSOI2008]火星人prefix
- centOS 7 设置固定IP,无法上外网
- List.toArray的使用
- 排列组合
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()《转载》
- C++调用外部应用程序的方法的整理总结
- python学习笔记三(scrapy)
- Linux查找进程并kill
- git建立追踪关系的小技巧
- webclient上传数据到ashx服务
- 关于基于http的嵌入式linux与服务器的get,post解析理解
- tomcat server.xml中文版