58 lines
1.6 KiB
C++
58 lines
1.6 KiB
C++
|
/**
|
|||
|
* File: subset_sum_i.cpp
|
|||
|
* Created Time: 2023-06-21
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD> I */
|
|||
|
void backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {
|
|||
|
// <20>Ӽ<EFBFBD><D3BC>͵<EFBFBD><CDB5><EFBFBD> target ʱ<><CAB1><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>
|
|||
|
if (target == 0) {
|
|||
|
res.push_back(state);
|
|||
|
return;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
|
|||
|
// <20><>֦<EFBFBD><D6A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> start <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>Ӽ<EFBFBD>
|
|||
|
for (int i = start; i < choices.size(); i++) {
|
|||
|
// <20><>֦һ<D6A6><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC>ͳ<EFBFBD><CDB3><EFBFBD> target <20><><EFBFBD><EFBFBD>ֱ<EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>ѭ<EFBFBD><D1AD>
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F2A3ACBA><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> target
|
|||
|
if (target - choices[i] < 0) {
|
|||
|
break;
|
|||
|
}
|
|||
|
// <20><><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><F1A3ACB8><EFBFBD> target, start
|
|||
|
state.push_back(choices[i]);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ѡ<EFBFBD><D1A1>
|
|||
|
backtrack(state, target - choices[i], choices, i, res);
|
|||
|
// <20><><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD>ָ<F1A3ACBB><D6B8><EFBFBD>֮ǰ<D6AE><C7B0>״̬
|
|||
|
state.pop_back();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD> I */
|
|||
|
vector<vector<int>> subsetSumI(vector<int> &nums, int target) {
|
|||
|
vector<int> state; // ״̬<D7B4><CCAC><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD>
|
|||
|
sort(nums.begin(), nums.end()); // <20><> nums <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int start = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
vector<vector<int>> res; // <20><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC>б<EFBFBD><D0B1><EFBFBD>
|
|||
|
backtrack(state, target, nums, start, res);
|
|||
|
return res;
|
|||
|
}
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
vector<int> nums = {3, 4, 5};
|
|||
|
int target = 9;
|
|||
|
|
|||
|
vector<vector<int>> res = subsetSumI(nums, target);
|
|||
|
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums = ";
|
|||
|
printVector(nums);
|
|||
|
cout << "target = " << target << endl;
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD>к͵<EFBFBD><EFBFBD><EFBFBD> " << target << " <20><><EFBFBD>Ӽ<EFBFBD> res = " << endl;
|
|||
|
printVectorMatrix(res);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|