Ones and Zeros

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

class Solution {
public:
    int findMaxForm(vector& strs, int m, int n) {
        vector> dp(m+1, vector(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)