Redian新闻
>
Combination Sum II哪里做错了
avatar
Combination Sum II哪里做错了# JobHunting - 待字闺中
c*a
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);
}
}
}
}
avatar
l*a
2
if there are duplicate in the input, you need to use
for(int i=start;iif(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.

avatar
c*a
3
[3,5,7], target 10
output [[3,7],[5,5]]
expected [[3,7]]
不知道为什么5会出现两次
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。