看看台湾名媛们的护肤怪癖 还真是有讲究# Fashion - 美丽时尚
a*l
1 楼
Given n items with size nums[i] which an integer array and all positive
numbers, no duplicates. An integer target denotes the size of a backpack.
Find the number of possible fill the backpack.
Each item may be chosen unlimited number of times
Example
Given candidate items [2,3,6,7] and target 7,
A solution set is:
[7]
[2, 2, 3]
return 2
class Solution {
public:
int backPackIV(vector& nums, int target) {
vector f(target + 1);
f[0] = 1;
// for (auto num : nums) {
// for (int i = 1; i <= target; ++i) {
// if (num <= i) f[i] += f[i-num];
// }
// }
// return f[target];
for (int i = 1; i <= target; ++i) {
for (auto num : nums) {
if (num <= i) f[i] += f[i-num];
}
}
return f[target];
}
};
注释掉的解法可以通过OJ,应该是正确的. 没注释掉的和注释掉的解法的不一样的地方
就是内外循环换了下, 求大神分析下为啥不行啊
numbers, no duplicates. An integer target denotes the size of a backpack.
Find the number of possible fill the backpack.
Each item may be chosen unlimited number of times
Example
Given candidate items [2,3,6,7] and target 7,
A solution set is:
[7]
[2, 2, 3]
return 2
class Solution {
public:
int backPackIV(vector
vector
f[0] = 1;
// for (auto num : nums) {
// for (int i = 1; i <= target; ++i) {
// if (num <= i) f[i] += f[i-num];
// }
// }
// return f[target];
for (int i = 1; i <= target; ++i) {
for (auto num : nums) {
if (num <= i) f[i] += f[i-num];
}
}
return f[target];
}
};
注释掉的解法可以通过OJ,应该是正确的. 没注释掉的和注释掉的解法的不一样的地方
就是内外循环换了下, 求大神分析下为啥不行啊