由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 今天做了挺难的一道题, 分享一下
相关主题
A家的题这个C++程序的运行结果是什么
一个容易记忆的permutation算法问个打印树的问题
CS: print all combination from an array关于结果除掉重复的问题请教
好记(但不是最优)的combination算法这些找missing number的题是不是都不能用求和做?
C++ Q42: (C22)问一个3 sum的问题
Amazon的Fibonacci题2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
C++ 一问?请教一道面试题
为什么我在array里用IsOdd 总出错?再来一个组合题吧
相关话题的讨论汇总
话题: ndup话题: nsum话题: int话题: vecres话题: index
进入JobHunting版参与讨论
1 (共1页)
w****x
发帖数: 2483
1
//Print All different Combinations of a Number as a Sum of Candidate Numbers
//Before start coding, several things to consider
//1. How to deal with duplicated numbers (sort them)
//2. What exactly is the successful ending condition
//3. What exactly is the failure ending condition
//4. For the container, pass by reference or by value
void _inner_print(int a[], int n, int nSum, vector& vecRes)
{
//the success ending condition is nSum == 0 and at the
//same time n == 0!! not nSum == 0 only
//consider 1, 2, -13, 13 ==> (3)
//another error logic is :
//if (nSum == 0) { print all; (no return here) }
//this logic will generate duplicated pairs
if (nSum == 0 && n == 0)
{
for (vector::iterator it = vecRes.begin();
it != vecRes.end(); it++)
cout<<*it<<" ";
cout< return;
}
if (n == 0) return;
int nDup = 1;
while (nDup < n && a[nDup-1] == a[nDup])
nDup++;
//choose 0 a[0]
_inner_print(a + nDup, n - nDup, nSum, vecRes);
for (int i = 1; i <= nDup; i++)
{
vecRes.push_back(a[0]);
nSum -= a[0];
_inner_print(a + nDup, n - nDup, nSum, vecRes);
}
//for reference, below logic is required
//think in this way, all called "_inner_print"
//must restore the vecRes' status as it passed in
for (int i = 1; i <= nDup; i++)
vecRes.pop_back();
}
void PrintAllComb(int a[], int n, int nSum)
{
assert(a && n > 0);
sort(a, a+n);
vector vecRes;
_inner_print(a, n, nSum, vecRes);
}
m***n
发帖数: 2154
2
哪儿找到的?
y****n
发帖数: 743
3
非递归解法。
void PrintCombinations(int[] numbers, int sum)
{
int[] numberCounts = new int[numbers.Length];
int index = 0;
int tempSum = sum;
bool subDone = false;
while (index >= 0)
{
if (index == numbers.Length - 1)
{
if (tempSum % numbers[index] == 0)
{
numberCounts[index] = tempSum / numbers[index];
// Print Combination
for (int i = 0; i < numbers.Length; i++)
for (int j = 0; j < numberCounts[i]; j++)
Console.Write(" " + numbers[i]);
}
index--;
subDone = true;
}
else if (subDone)
{
tempSum -= numbers[index];
numberCounts[index]++;
subDone = false;
}
else if (tempSum >= 0)
{
index++;
}
else
{
tempSum += numberCounts[index] * numbers[index];
numberCounts[index] = 0;
index--;
subDone = true;
}
}
}
w****x
发帖数: 2483
4
太扯了, 这个帖子也能被推荐???
i*********7
发帖数: 348
5
还没看代码解答。不是特别清楚楼主的题目的意思。
能够稍微再说清楚一点吗?给个example
w****x
发帖数: 2483
6

1,3,4,5,-4,0,0 求所有和等于0的subset, subset不能重复
4, -4, 0
4, -4
4, -4, 0, 0
0
0, 0

【在 i*********7 的大作中提到】
: 还没看代码解答。不是特别清楚楼主的题目的意思。
: 能够稍微再说清楚一点吗?给个example

1 (共1页)
进入JobHunting版参与讨论
相关主题
再来一个组合题吧C++ Q42: (C22)
combination sum这题的复杂度?Amazon的Fibonacci题
F面经C++ 一问?
combinations II 怎么搞为什么我在array里用IsOdd 总出错?
A家的题这个C++程序的运行结果是什么
一个容易记忆的permutation算法问个打印树的问题
CS: print all combination from an array关于结果除掉重复的问题请教
好记(但不是最优)的combination算法这些找missing number的题是不是都不能用求和做?
相关话题的讨论汇总
话题: ndup话题: nsum话题: int话题: vecres话题: index