首页 > 代码库 > LeetCode_39combinationSum2 [Combination Sum II]

LeetCode_39combinationSum2 [Combination Sum II]

#pragma warning(disable:4996)

#include <Windows.h>
#include <tchar.h>
#include <cstdio>
#include <vector>
using namespace std;

/*
	submit time : 3
		1. Runtime Error
			Last executed input:	[5,3], 5
		2. Wrong Answer
			Input:	[2,2,2], 4
			Output:	[[2,2],[2,2]]
			Expected:	[[2,2]]
	request :
		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]
*/

//=================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 helpCombinationSum2(vector<vector<int> >& result, vector<int>& solution, int* pStart, int* pEnd, int* vernier, int target, bool choose) {
	if (target == 0) {
		result.push_back(solution);
		return;
	}
	if ((vernier <= pEnd && target < *vernier) || (vernier > pEnd))
		return;

	int currValue = http://www.mamicode.com/*vernier;>