leetcode-master/problems/0377.组合总和Ⅳ.md

47 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

和之前个回溯法的各个总和串起来一波,本题用回溯稳稳的超时
```
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 0; i <= target; i++) {
for (int j = 0; j < nums.size(); j++) {
if (i - nums[j] >= 0) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
};
```
C++测试用例有超过两个树相加超过int的数据
超限的情况
一些题解会直接用ull usigned long long
java 也是四个字节理论上没有差别可能后台java和C++测试用例不同bug.....
```
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 0; i <= target; i++) {
for (int num : nums) {
if (i - num >= 0 && dp[i] < INT_MAX - dp[i - num]) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
};
```