由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Combination Sum II哪里做错了
相关主题
请教leetcode Combination Sum II的code,谢谢。combination sum II
问道leetcode的题:Combination Sum II请教一道google面试题
a problem from leetcode: high efficiency algorithm for combinations problem发个Palantir的电面,并求g家onsite的bless
combinations 有没有 iterative的方法阿 ?问一道leetcode上的题目 combination sum
关于结果除掉重复的问题请教请教一个题目
CS: print all combination from an array请教leetcode Subsets II
问个递归的问题请教一下subset I 输出子集顺序问题
问个算法题2Facebook Phone Inteview + 流程请教
相关话题的讨论汇总
话题: arraylist话题: integer话题: int话题: sum话题: buff
进入JobHunting版参与讨论
1 (共1页)
c*****a
发帖数: 808
1
大牛快来看看
Combination Sum II
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.
Elements in a combination (a1, a2, … , ak) must be in non-descending
order. (ie, a1 ≤ a2 ≤ … ≤ ak).
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]
public class Solution {
public ArrayList> combinationSum2(int[] num, int
target) {
// Start typing your Java solution below
// DO NOT write main() function
int []buff = new int[num.length];
ArrayList> res = new ArrayList
>();
if(num.length==0) return res;
Arrays.sort(num);
comb(res,buff, num, target, 0);
return res;
}
public void comb(ArrayList> res, int[]buff, int []S,
int sum, int index){
if(sum==0){
ArrayList subset = new ArrayList();
for(int i =0; i subset.add(buff[i]);
}
Collections.sort(subset);
if(!res.contains(subset))
res.add(subset);
}
else
if(sum<0 || index == S.length) return;
else{
for(int i=index;i=0;i++){
buff[index] = S[i];
comb(res,buff,S,sum-S[i], index+1);
}
}
}
}
l*****a
发帖数: 14598
2
if there are duplicate in the input, you need to use
for(int i=start;i<**.length;i++) {
if(i!=start && **[i]==**[i-1]) continue;
....
}

【在 c*****a 的大作中提到】
: 大牛快来看看
: Combination Sum II
: 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.
: Elements in a combination (a1, a2, … , ak) must be in non-descending
: order. (ie, a1 ≤ a2 ≤ … ≤ ak).
: The solution set must not contain duplicate combinations.

c*****a
发帖数: 808
3
[3,5,7], target 10
output [[3,7],[5,5]]
expected [[3,7]]
不知道为什么5会出现两次
1 (共1页)
进入JobHunting版参与讨论
相关主题
Facebook Phone Inteview + 流程请教关于结果除掉重复的问题请教
这题也可以DP 解吧?CS: print all combination from an array
关于leetcode的combinationSum题问个递归的问题
求个4sum的算法问个算法题2
请教leetcode Combination Sum II的code,谢谢。combination sum II
问道leetcode的题:Combination Sum II请教一道google面试题
a problem from leetcode: high efficiency algorithm for combinations problem发个Palantir的电面,并求g家onsite的bless
combinations 有没有 iterative的方法阿 ?问一道leetcode上的题目 combination sum
相关话题的讨论汇总
话题: arraylist话题: integer话题: int话题: sum话题: buff