Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8
A solution set is:

[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

这道题就是39题的变化版本,这里每一个数字只许出现一次,并且最后的结果不允许重复,第一次只是将上一题的代码修改了边界条件,但结果不是很理想。

public class combinationSum2 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
getResult(candidates,target,0,result,new ArrayList<Integer>());
return result;
} public void getResult( int[] candidates, int target,int pos, List<List<Integer>> result,List<Integer> ans){
for( int i = pos;i <candidates.length; i++){
if( target == candidates[i]){
ans.add(candidates[i]);
result.add(new ArrayList<Integer>(ans));
ans.remove(ans.size()-1);
return;
}
else if(target > candidates[i]){ ans.add(candidates[i]);
getResult(candidates,target-candidates[i],i+1,result,ans);
ans.remove(ans.size()-1);
}else
return ;
}
}
/*
* 1.这道题和conbinationSum很相似,只不过不能使用重复的数字。
* 2.35+9
*/
}

之后发现,如果在getResult中,用数组代替List<Integer>那么会快很多,修改之后,做到了最快。

public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
getResult(candidates,target,0,result,new int[candidates.length],0);
return result;
} public void getResult( int[] candidates, int target,int pos, List<List<Integer>> result,int[] ans,int num){
for( int i = pos;i <candidates.length; i++){
if( target == candidates[i]){
List<Integer> aa = new ArrayList<Integer>();
for( int ii =0; ii<num; ii++)
aa.add(ans[ii]);
aa.add(candidates[i]);
result.add(aa);
return;
}
else if(target > candidates[i]){
ans[num] = candidates[i];
getResult(candidates,target-candidates[i],i+1,result,ans,num+1);
while( i+1< candidates.length && candidates[i] == candidates[i+1])
i++; }else
return ;
}
}
}

leetcode 40 Combination Sum II --- java的更多相关文章

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. [LeetCode] 40. Combination Sum II 组合之和 II

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

  3. [leetcode]40. Combination Sum II组合之和之二

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

  4. [LeetCode] 40. Combination Sum II 组合之和之二

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

  5. 40. Combination Sum II (JAVA)

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

  6. Java [Leetcode 40]Combination Sum II

    题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...

  7. LeetCode 40. Combination Sum II (组合的和之二)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. Leetcode 40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

随机推荐

  1. COleDateTime类型的应用

    使用COleDateTime类1) 获取当前时间.      CTime time;      time = CTime::GetCurrentTime();2) 获取时间元素.      int y ...

  2. JDBC 连接池

    数据库连接池(connection pool) JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接 ...

  3. python框架(flask/django/tornado)比较

    一.对外数据接口 三者作为web框架,都是通过url映射对外的接口 flask:以decorator的形式,映射到函数中 django:以字典形式,映射到函数 tornado: 以字典形式,映射到类中 ...

  4. JAVA每日一记

    1.两个最基本的java回收算法:复制算法和标记清理算法                 复制算法:两个区域A和B,初始对象在A,继续存活的对象被转移到B.此为新生代最常用的算法            ...

  5. Android中 服务里的方法抽取成接口

    1 写个类继承Service 重写 onBind方法 返回一个IBinder 对象(传递到连接成功时用) 2 服务中 写一个内部类 继承IBinder 并且实现一个接口(用于抽取方法)继承IBinde ...

  6. 【python】import 模块、包、第三方模块

    xx.py文件,称为模块(module),把不同模块归整到一起的文件夹,叫做包(package) 不同包下的模块可以重名,但是都不能和系统内建模块重名 包里面一定要有个__init__.py文件,否则 ...

  7. Android布局— — —帧布局

    帧布局,开发中很少使用,最简单的布局 添加多个控件,这些控件会按顺序在屏幕左上角重叠显示,且会透明显示之前控件的文本语法格式:<?xml version="1.0" enco ...

  8. 码表由来:ascll码-Gbk2312-GBK-Unicode-UTF-8

    码表ascll码-Gbk2312-GBK-Unicode-UTF-8, ascll是基本的标准码表,GB2312是中文码表,GBK是扩展之后的码表,Unicode是国际通用码表,UTF-8是优化后的U ...

  9. operation not possible due to RF-kill

    使用mdk3时出现这个问题operation not possible due to RF-kill 就是输入第一条命令 后出现 operation not possible due to RF-ki ...

  10. Ubuntu 14.10 下sed命令详解

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...