Leetcode_38combinationsum [combination sum]

Source: Internet
Author: User
#pragma warning(disable:4996)#include <cstdio>#include <tchar.h>#include <Windows.h>#include <vector>using namespace std;/*submit time : 41.Runtime ErrorLast executed input:[1], 2// Find it2. Wrong AnswerInput:[2,3,5], 7Output:[[2,2,3]]Expected:[[2,2,3],[2,5]]3. Wrong AnswerInput:[1,2], 2Output:[[1,1],[2],[2]]Expected:[[1,1],[2]]request :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]*///=================QuickSort===================void Swap(int* data, int i, int j) {int temp = data[i];data[i] = data[j];data[j] = temp;}int findpivot(int* data, int i, int j) {return (i + j) >> 1;}int partition(int* data, int l, int r, int pivot) {while (l < r) {while (data[++l] < pivot);while (l<r && data[--r] > pivot);Swap(data, l, r);}return l;}void qsort(int* data, int i, int j) {if (j <= i) return;int pivotIndex = findpivot(data, i, j);Swap(data, pivotIndex, j);int k = partition(data, i - 1, j, data[j]);Swap(data, k, j);qsort(data, i, k - 1);qsort(data, k + 1, j);}//===================END=======================void helpCombinationSum(vector<vector<int> >& result, vector<int>& solution, int* pStart, int* pLast, int* vernier, int target) {if (target == 0) {result.push_back(solution);return;}if ((vernier <= pLast && target < *vernier) || (vernier > pLast))return;int currValue = *vernier;int maxCount = target / currValue;int rem = target % currValue;int i = maxCount;while (i) {solution.push_back(currValue);--i;}while (maxCount >= 0) {helpCombinationSum(result, solution, pStart, pLast, vernier + 1, target - currValue*maxCount);if (maxCount)solution.pop_back();--maxCount;}}vector<vector<int> > combinationSum(vector<int> &candidates, int target) {vector<vector<int> > result;if (candidates.size() == 0) return result;vector<int> solution;int* vernier = &candidates[0];int length = candidates.size();int* firstCandidate = vernier;int* lastCandidate = vernier + length - 1;// sort candidatesqsort(vernier, 0, length - 1);// submit failed here/*while (vernier <= lastCandidate) {helpCombinationSum(result, solution, firstCandidate, lastCandidate, vernier, target);++vernier;}*/helpCombinationSum(result, solution, firstCandidate, lastCandidate, vernier, target);return result;}//======================Test========================void printVector(vector<int>& num) {vector<int>::iterator iter = num.begin();for (; iter != num.end(); ++iter)printf("%d ", *iter);printf("\n");}void Test() {//int data[] = { 9, 31, 25, 5, 8, 6, 16 };int data[] = { 2,3,5 };int target = 7;vector<int> candidates(data, data + sizeof(data) / sizeof(int));vector<vector<int> > result = combinationSum(candidates, target);vector<vector<int> >::iterator iter = result.begin();for (; iter != result.end(); ++iter)printVector(*iter);}int _tmain(int argc, _TCHAR* argv[]) {Test();system("pause");return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.