Section A
#include <functional>
#include <iostream>
int main() {
int a = 5, b = -5;
auto lmbd = [&](int a) -> void {
--a;
--b;
};
std::function<void(int)> func = [&](int a) -> void {
--a;
--b;
};
func(a);
std::cout << a << ' ' << b << std::endl;
return 0;
}
Section B
698. 划分为k个相等的子集 - 力扣(LeetCode)(超时写法)
class Solution {
public:
bool canPartitionKSubsets(vector<int> &nums, int k) {
const int n = nums.size();
size_t sum = 0;
for (auto num : nums) {
sum += num;
}
if (sum % k != 0) {
return false;
}
const int t = sum / k;
bool flag = false;
function<void(int, int)> func = [&](int mask, int cur) -> void {
if (flag || cur > t) {
return;
} else if (cur == t) {
--k;
cur = 0;
if (k == 0) {
flag = true;
return;
}
}
for (int i = 0; i < nums.size(); ++i) {
if ((mask & (1 << i)) == 0) {
func(mask | (1 << i), cur + nums[i]);
}
}
};
func(0, 0);
return flag;
}
};