美文网首页
Ones and Zeros

Ones and Zeros

作者: pretzei | 来源:发表于2017-03-02 21:29 被阅读0次

    才发现别人用的是mardown zzz
    题目地址:https://leetcode.com/problems/ones-and-zeroes/?tab=Description
    还是简单dp。题意是给出一定数量的0和1,以及一个01字符串的数组,问这些01能组成多少个字符串。字符串是幌子,先计算里面多少01。然后直接dp

    class Solution {
    public:
        int findMaxForm(vector<string>& strs, int m, int n) {
            vector<vector<int>> dp(m+1, vector<int>(n+1, 0));
            for (string str : strs) {
                int zero = 0, one = 0;
                for (char charector : str) {
                    if (charector == '0') {
                        zero++;
                    } else {
                        one++;
                    }
                }
                for (int i = m; i >= zero; i--) {
                    for (int j = n; j >= one; j--) {
                        dp[i][j] = max(dp[i][j], dp[i-zero][j-one]+1);
                    }
                }
            }
            return dp[m][n];
        }
    };
    

    果然好看多了~smile

    相关文章

      网友评论

          本文标题:Ones and Zeros

          本文链接:https://www.haomeiwen.com/subject/kywkgttx.html