forked from youngyangyang04/leetcode-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77f45df
commit 9f6e4e9
Showing
4 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
搞不懂 leetcode后台是什么牛逼的编译器,初始化int dp[101][101] = {0}; 可以 ,int dp[101][101];就不行,有其他默认值,坑死。 | ||
代码我做了实验,后台会拿findMaxForm,运行两次,取第二次的结果,dp有上次记录的数值。 | ||
|
||
``` | ||
// 即使做了很多动态规划的题目,做这个依然懵逼 | ||
// 这道题目有点 程序员自己给自己出难进急转弯的意思 | ||
// 该子集中 最多 有 m 个 0 和 n 个 1 。 指的是整体子集 | ||
// 这是二维背包,多重背包 | ||
// dp[i][j] 有i个0,j个1最大有多少个子集,但是遍历的时候 顶部是哪里呢? | ||
class Solution { | ||
public: | ||
int findMaxForm(vector<string>& strs, int m, int n) { | ||
int dp[101][101] = {0}; // 默认初始化0 | ||
for (int i = 0; i < strs.size(); i++) { | ||
int oneNum = 0, zeroNum = 0; | ||
for (char c : strs[i]) { | ||
if (c == '0') zeroNum++; | ||
else oneNum++; | ||
} | ||
// 果然还是从后向前,模拟01背包 | ||
for (int j = m; j >= zeroNum; j--) { | ||
for (int k = n; k >= oneNum; k--) { | ||
dp[j][k] = max(dp[j][k], dp[j - zeroNum][k - oneNum] + 1); | ||
} | ||
} | ||
} | ||
return dp[m][n]; | ||
} | ||
}; | ||
``` |