由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode上一题,求正解
相关主题
C的argc问题为什么我这段简单的程序segment fault
一道C语言题c++ 程序一问
请教一个入门级的C的指针问题bloomberg assessment的机经,c语言的(20道题)
背包问题C++ online Test 又一题
请问为什么这个程序会出现RunTime ErrorC++ 一题
combination sum II这题哪错了?
你们看过programming pearls (2nd edition English) or 正在看的同学们这个看着很白痴的问题有更好的解法吗?
one c++ question帮看看这段code
相关话题的讨论汇总
话题: int话题: vector话题: start话题: sum话题: candidates
进入JobHunting版参与讨论
1 (共1页)
l**********1
发帖数: 415
1
Given a set of candidate numbers (C) and a target number (T), find all
unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
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 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
q********c
发帖数: 1774
2
帖个我写的,欢迎指正。
void getCandidates(int data[], int size, int sum)
{
vector list;
for(int i = 0; i < size; ++i) {
list.clear();
getSums(data, size, i, sum, list);
}
}
void getSums(int data[], int size, int start, int k, vector& list)
{

if(k == 0)
printList(list);
else if(k < 0)
return;
else{
list.push_back(data[start]);
k = k - data[start];
for(int j = start; j < size; ++j) {
if(k == data[j]) {
list.push_back(data[j]);
printList(list);
list.pop_back();
}
}
if(k >= 0) {
getSums(data, size, start, k, list);
}
list.pop_back();
}
}
void printList(vector& list)
{
for(int i = 0; i < list.size(); ++i){
cout << list[i] << " ";
}
cout < }
s******n
发帖数: 3946
3
写了几遍才对。。。
#include
#define exchangenum(a,b) {a^=b;b^=a;a^=b;}
void print(int* a, int count, int sum, int* out, int outindex)
{
if (sum==0) {
for (int i=0; i printf("\n");
return;
}
if (count==0) return;
out[outindex] = a[0];
print(a+1, count-1, sum-a[0], out, outindex+1);
print(a+1, count-1, sum, out, outindex);
}
int main(int argc, char** argv)
{
int a[5] = {1,3,4,2,6};
int sum = 7;
int out[5];
print(a, sizeof(a)/sizeof(a[0]), sum, out, 0);
}
l**********1
发帖数: 415
4
貌似这种的时间复杂度极高啊,而且用recursion的话,空间复杂度也很大啊。有没有
什么巧妙的算法呢?像2sum可以在O(nlogn) constant space得到。还是巧妙的算法对于
interview太过复杂,这个就足够了?谢谢!
i**********e
发帖数: 1145
5
楼上的几位都没处理重复,output 仍然有重复组合。
j*****j
发帖数: 201
6
我的解法, test case都通过了
vector > combinationSumrec(vector candidates, int start,
int end,int target){
vector > toreturn;
if (target == 0){
vector temp;
toreturn.push_back(temp);
return toreturn;
}
if (target return toreturn;
if (start > end)
return toreturn;
if (start == end){
if (target%(candidates[start]) == 0){
int product = target/candidates[start];
vector temp;
for (int k=0;k temp.push_back(candidates[start]);
toreturn.push_back(temp);
}
return toreturn;
}
vector > nolast = combinationSumrec(candidates, start,
end-1,target);
vector > withlast = combinationSumrec(candidates, start,
end,target-candidates[end]);
for(int i=0;i withlast[i].push_back(candidates[end]);
toreturn.push_back(withlast[i]);
}
for(int j=0;j toreturn.push_back(nolast[j]);
}
return toreturn;

}
vector > combinationSum(vector &candidates, int target)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > allsum;
if (candidates.size() == 0)
return allsum;
sort(candidates.begin(),candidates.end());
allsum = combinationSumrec(candidates,0,candidates.size()-1,target);
return allsum;
}

【在 i**********e 的大作中提到】
: 楼上的几位都没处理重复,output 仍然有重复组合。
s******n
发帖数: 3946
7
如果有1,3,3,4,5,3重复,则需要特别处理,2个连续的3,分别处理3,3,4... 3,4... ,4.... 三种情况,应该没比我这程序更简短的了吧。:)
#include
void print(int* a, int count, int sum, int* out, int outindex)
{
if (sum==0) {
for (int i=0; i printf("\n");
return;
}
if (sum<0 || count==0) return;
int samea=1;
for (; samea for (int k=0; k<=samea; k++) {
print(a+samea, count-samea, sum-a[0]*(k), out, outindex+k);
out[outindex+k] = a[0];
}
}
int main(int argc, char** argv)
{
int a[6] = {1,2,3,3,4,6};
int sum = 8;
int out[6];
print(a, sizeof(a)/sizeof(a[0]), sum, out, 0);
}

【在 i**********e 的大作中提到】
: 楼上的几位都没处理重复,output 仍然有重复组合。
a***g
发帖数: 234
8
这个和硬币算钱是一样的么
z*****n
发帖数: 447
9
强!学习了:)
另外,要先sort一下,不然会有重复。

. ,4.... 三种情况,应该没比我这程序更简短的了吧。:)

【在 s******n 的大作中提到】
: 如果有1,3,3,4,5,3重复,则需要特别处理,2个连续的3,分别处理3,3,4... 3,4... ,4.... 三种情况,应该没比我这程序更简短的了吧。:)
: #include
: void print(int* a, int count, int sum, int* out, int outindex)
: {
: if (sum==0) {
: for (int i=0; i: printf("\n");
: return;
: }
: if (sum<0 || count==0) return;

i**********e
发帖数: 1145
10
很好,不过你这个是解决另一个问题的变种。(还有一个小bug会产生重复,例如
[2,5,2,1,2], sum=5, 你的答案是 [[2,1,2],[5],[2,1,2],[2,2,1]], 有多个重复。
只要排序一下就好了)。
问题里说明:
The same repeated number may be chosen from C unlimited number of times.
也就是说同一个元素可以取出多次。不过这个思路也很类似。
这题的总结在这里:
http://www.leetcode.com/2010/09/print-all-combinations-of-numbe

. ,4.... 三种情况,应该没比我这程序更简短的了吧。:)

【在 s******n 的大作中提到】
: 如果有1,3,3,4,5,3重复,则需要特别处理,2个连续的3,分别处理3,3,4... 3,4... ,4.... 三种情况,应该没比我这程序更简短的了吧。:)
: #include
: void print(int* a, int count, int sum, int* out, int outindex)
: {
: if (sum==0) {
: for (int i=0; i: printf("\n");
: return;
: }
: if (sum<0 || count==0) return;

1 (共1页)
进入JobHunting版参与讨论
相关主题
帮看看这段code请问为什么这个程序会出现RunTime Error
问一道kth smallest element的题目combination sum II
写了一个find kth number in 2 sorted arrays的code 请大牛看你们看过programming pearls (2nd edition English) or 正在看的同学们
一题one c++ question
C的argc问题为什么我这段简单的程序segment fault
一道C语言题c++ 程序一问
请教一个入门级的C的指针问题bloomberg assessment的机经,c语言的(20道题)
背包问题C++ online Test 又一题
相关话题的讨论汇总
话题: int话题: vector话题: start话题: sum话题: candidates