Combination and Permutation
来源:互联网 发布:淘宝有哪些女装潮店 编辑:程序博客网 时间:2024/06/06 04:29
码了几个题,都是backtrack 的问题变形
主要涉及的还是 combination and permutation 形式基本相同
稍微写一下
懒
首先是combination, 就是从n个数里面选k个,先挑一个出来,再从剩下的n-1个挑 k-1个或者k个
public List<List<Integer>> combine(int n, int k) { List<List<Integer>> list = new ArrayList<>(); backtrace(n,k,list,new ArrayList<>());//helper method return list; } private void backtrace (int n, int k, List<List<Integer>> list, List<Integer> present){ if(k<=0){//已经挑出来k个了结束 list.add(new ArrayList(present));//退出递归的条件 }else{ for(int i = n; i >0; i--){ present.add(i); backtrace(i-1,k-1,list,present);//包含i的情况 present.remove(present.size() - 1);//删除i的情况 } } }
然后是permutation,和combination不同的是,所有的元素都要参与
public List<List<Integer>> combine(int n, int k) { List<List<Integer>> list = new ArrayList<>(); backtrace(n,k,list,new ArrayList<>()); return list; } private void backtrace (int n, int k, List<List<Integer>> list, List<Integer> present){ if(present.size()==n){//只有到present序列的长度和全部元素的个数相同的时候才结束 list.add(new ArrayList(present)); }else{ for(int i = n; i >0; i--){//每一次都把所有元素遍历一遍 与combination不同的是,combination里面n是递减的 if(present.contains(i)) continue;//这一步在permutation里面就是跳过自己 present.add(i); backtrace(n,k-1,list,present);//实际上这里不需要n。。只是直接从combination改过来的 present.remove(present.size() - 1); } } }
在combinationsum2的问题里面
是这样的
public class Solution {public List<List<Integer>> combinationSum2(int[] nums, int target) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, target, 0); return list; }private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){ if(remain < 0) return; else if(remain == 0) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < nums.length; i++){ if(i > start && nums[i] == nums[i-1]) continue; // ~~!!不一样的点在这 tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i + 1); tempList.remove(tempList.size() - 1); } }} }
首先这个nums是sorted所以如果有相同数字就连在一块了
比如说输入 11223 要求sum是5 那么就需要避免 122 122 这种情况,其实是不一样的1 和2 但是结果是duplicate的
阅读全文
0 0
- Combination and Permutation
- Permutation & Combination
- Java版排列组合工具类 - Java Permutation and Combination Tools
- Subsets, Combination, Permutation, Combination Sum
- [LeetCode] Combination, Permutation, subsets
- Permutation,Subset,Combination…
- java 实现排列组合Permutation and Combination和链式不相邻排列组合和环形不相邻排列
- precedence and combination
- B. Levko and Permutation
- Codeforces554D:Kyoya and Permutation
- hdoj4985Little Pony and Permutation
- Nicholas and Permutation
- Nicholas and Permutation
- A. Nicholas and Permutation
- [codeforces]Levko and Permutation
- permutation and its variations
- Bit Reversal and Permutation
- Anton and Permutation CF
- Day 9 linux基础 samba及Nginx服务
- MapReduce实例----单表关联
- 设计模式
- Ubuntu16.04安装x11VNC远程桌面
- 理解ROS话题(六)
- Combination and Permutation
- Linux 内核剖析
- Android性能优化之避免ANR
- leofs manager(三)
- Python多线程之event
- spring是用aop:before报错解决方法
- [Leetcode] 168. Excel Sheet Column Title 解题报告
- tcpdump使用与Wireshark抓包分析
- Server