combination sum Ipermutation Isubsets  I 是组合和、全排列、子集的第一种情况,给定数组中没有重复的元素。

combination sum IIpermutation IIsubsets  II 是组合和、全排列、子集的第而种情况,给定数组中有重复元素。

combination sum I中元素每个可以被多次使用,所以每次遍历都是从当前元素开始,然后往后面遍历。

combination sum II中元素只能被使用一次,所以算下个元素时,只能从当前元素后面的元素查找。

全排列每次都要从头开始遍历,既然从头遍历,就要考虑前面元素是否使用过。对于permutation I,每次从头遍历只要判断当前元素是否使用过,这个可以使用list.contains判断,也可以使用combination sum II而中的数组来表示是否使用过。因为全排列每次从头遍历,而且添加时不能添加那些已经在集合中的元素(使用过的),所以用一个数组。而从当前元素下一个开始遍历的情况(求子集),则不需要考虑前面的元素,只考虑后面的,而后面的元素都是没有使用过的,所以不需要再判断是否使用过,只需要考虑跳过重复元素就行。

求子集是每次要从当前元素后面的元素添加,也就是遍历当前元素后面的元素。subsets  I不需要判断是否重复,也不需要排序。判断条件只是list中元素个数小于nums长度即可,也就是是集合之一,但是判断完了不返回,因为还有继续添加。subsets  II则要排序,还有去除重复元素,这里跳过重复元素并不需要额外数组,因为不是从头开始,只要跳过相同的即可。

注意这几个处理重复的方法:全排列是用到所有元素,每次从头遍历,所以它处理重复的方法就是使用一个数组标记是否使用过该元素。子集和组合不是从头开始遍历,每次只遍历后面的元素,它的处理方式就是直接用if跳过相同元素。

代码:

combination sum I

class Solution {
public List<List<Integer>> combinationSum(int[] nums, int target) {
/**
这个题是穷举所有情况,回溯的可能性比较大
*/
Arrays.sort(nums);
List<List<Integer>> result=new ArrayList<List<Integer>>();
backtracking(result,new ArrayList<Integer>(),nums,target,0,0);
return result;
} public void backtracking(List<List<Integer>> result,List<Integer> tmp,int[] nums,int target,int sum,int index){
if(sum>target) return ;
if(sum==target) {
result.add(new ArrayList<Integer>(tmp));
return ;
} if(index>nums.length-1) return ; for(int i=index;i<nums.length;i++){
tmp.add(nums[i]);
backtracking(result,tmp,nums,target,sum+nums[i],i);
tmp.remove(tmp.size()-1);
}
}
}

combination sum II

class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result=new ArrayList<List<Integer>>();
if(candidates==null||candidates.length==0) return result;
Arrays.sort(candidates);
backtracking(result,new ArrayList<Integer>(),candidates,target,0,0);
return result;
} public void backtracking(List<List<Integer>> result,List<Integer> list,int[] nums,int target,int sum,int index){
if(sum>target) return ;
if(sum==target){
result.add(new ArrayList<Integer>(list));
return ;
}
if(index>nums.length-1) return ;
/*
这一题相比combination sum I,做了两点修改,1、数组中每个元素只能被使用一次;2、数组中有重复元素。针对1,每次添加list从下一个元素开始(i+1);针对2,每次遍历的时候,跳过重复的元素。
*/
for(int i=index;i<nums.length;i++){
//每次遍历时,跳过重复元素,这样就不会出现重复的两个list
if(i>index&&nums[i]==nums[i-1]) continue;
list.add(nums[i]);
backtracking(result,list,nums,target,sum+nums[i],i+1);
list.remove(list.size()-1);
}
}
}

permination I

class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(nums.length<=0) return result;
backtracking(result,new ArrayList<Integer>(),nums);
return result;
} public void backtracking(List<List<Integer>> result, List<Integer> list,int[] nums){
if(list.size()==nums.length){
result.add(new ArrayList<Integer>(list));
return ;
} for(int i=0;i<nums.length;i++){
if(list.contains(nums[i])) continue;//每次都是遍历数组中的每个元素,然后添加不一样的元素(保证不重复)
list.add(nums[i]); backtracking(result,list,nums);
list.remove(list.size()-1); }
}
}

permination II

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;
//该数组用来标记每个位置的元素是否使用以保证每个位置的元素只能使用一次。控制递归遍历(往list后面添加元素)中该元素是否已经使用。
boolean[] used=new boolean[nums.length];
Arrays.sort(nums);
List<Integer> list=new ArrayList<>();
helper(nums,res,list,used);
return res;
} public void helper(int[] nums,List<List<Integer>> res,List<Integer> list,boolean[] used){
if(nums.length==list.size()){
res.add(new ArrayList<Integer>(list));
return ;
}
for(int i=0;i<nums.length;i++){
if(used[i]) continue; //该元素使用过了。
//下一个重复值只有在前一个重复值被使用的情况下才可以使用。
/*
    这个!used[i-1]条件是为深度遍历准备的。
对于当前层遍历时,每遍历一个元素结束后就会设为false然后遍历下一个,所以可以跳过重复元素,避免相同元素出现在同一个位置。
对于深度遍历(递归,往结果集中继续添加元素),需要使用!used[i-1]来保证后面的相同元素可以往后面的结果集中添加。
      比如[1,1,2].第一层遍历第一个1时,设为true后,递归往后面添加第二个元素,此时也是从第一个元素开始遍历,
      因为此时used[0]为true,所以跳过,i=1,此时如果没有!used[i-1],也会跳过第二个1了,所以需要加上这个条件。
      
      */ if(i>0&&nums[i]==nums[i-1]&&!used[i-1]) continue;
used[i]=true;
list.add(nums[i]);
helper(nums,res,list,used);
//下面这两步,为了同层遍历,同层遍历是给一个位置轮流添加元素。所以需要将上一个添加的元素删了,然后删除标记,遍历下一个元素
used[i]=false;
list.remove(list.size()-1);
}
}
}

subsets I

class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(nums==null||nums.length==0) return res;
helper(res,new ArrayList<Integer>(),nums,0);
return res;
} public void helper(List<List<Integer>> res,List<Integer> list,int[] nums,int index){
if(list.size()<=nums.length){
res.add(new ArrayList<Integer>(list));
}
for(int i=index;i<nums.length;i++){
list.add(nums[i]);
helper(res,list,nums,i+1);
list.remove(list.size()-1);
}
}
}

subsets II

class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(nums==null||nums.length==0) return res;
Arrays.sort(nums); //有相同元素,所以要排序
helper(res,new ArrayList<Integer>(),nums,0);
return res;
}
public void helper(List<List<Integer>> res,List<Integer> list,int[] nums,int index){
//求子集,所以判断条件为.而且满足条件后还要继续向下进行,而不是返回
if(list.size()<=nums.length)
res.add(new ArrayList<Integer>(list));
for(int i=index;i<nums.length;i++){
//跳过相同的元素。因为是从后面的元素中选元素,不是从头开始,所以不需要再创建是否使用的数组了。注意:下面是i>index,而不是i>0.
if(i>index&&nums[i]==nums[i-1]) continue;
list.add(nums[i]);
helper(res,list,nums,i+1);
list.remove(list.size()-1);
}
}
}

combination sum、permutation、subset(组合和、全排列、子集)的更多相关文章

  1. 【LeetCode每天一题】Combination Sum II(组合和II)

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  2. 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

    引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...

  3. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  4. [Leetcode 39]组合数的和Combination Sum

    [题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...

  5. [Leetcode 216]求给定和的数集合 Combination Sum III

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  6. 【LeetCode练习题】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  7. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  8. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  9. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

随机推荐

  1. AndroidStudio如何快速制作.so

    之前写过一篇Eclipse制作.so的文章,http://blog.csdn.net/baiyuliang2013/article/details/44306921使用的是GNUstep模拟Linux ...

  2. Collections类解析

    最常用的排序: 需要实现Comparable接口 1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它 ...

  3. A*寻路算法入门(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  4. 取KindEditor中的textarea的值区不到的解决方案,固定kindEditor的高度

     可以通过下面的方式取到textarea的值 var content = $(document.getElementsByTagName('iframe')[0].contentWindow.do ...

  5. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  6. 使用UE4/Unity创建VR项目

    一.主要的步骤是说一下使用UE4,在此之前先说一下使用unity创建的VR项目 1.unity创建oculus rift dk2项目 在unity中创建一个简单的场景,让摄像机能看见场景中的物体,不对 ...

  7. Mybatis插件原理分析(二)

    在上一篇中Mybatis插件原理分析(一)中我们主要介绍了一下Mybatis插件相关的几个类的源码,并对源码进行了一些解释,接下来我们通过一个简单的插件实现来对Mybatis插件的运行流程进行分析. ...

  8. 【翻译】使用Sencha Ext JS 6打造通用应用程序

    原文:Using Sencha Ext JS 6 to Build Universal Apps {.aligncenter} 在Sencha和整个Ext JS团队的支持下,我很高兴能跟大家分享一下有 ...

  9. linux 下停止java jar包 shell

    linux 下停止java jar包 shell http://injavawetrust.iteye.com #!/bin/sh APP_HOME=/home/ap/injavawetrust/ba ...

  10. ORACLE里锁有以下几种模式,v$locked_object,locked_mode

    ORACLE里锁有以下几种模式: 0:none 1:null 空 2:Row-S 行共享(RS):共享表锁,sub share  3:Row-X 行独占(RX):用于行的修改,sub exclusiv ...