Java | Leetcode Java题解之第474题一和零
题目:
题解:
class Solution {public int findMaxForm(String[] strs, int m, int n) {int[][] dp = new int[m + 1][n + 1];int length = strs.length;for (int i = 0; i < length; i++) {int[] zerosOnes = getZerosOnes(strs[i]);int zeros = zerosOnes[0], ones = zerosOnes[1];for (int j = m; j >= zeros; j--) {for (int k = n; k >= ones; k--) {dp[j][k] = Math.max(dp[j][k], dp[j - zeros][k - ones] + 1);}}}return dp[m][n];}public int[] getZerosOnes(String str) {int[] zerosOnes = new int[2];int length = str.length();for (int i = 0; i < length; i++) {zerosOnes[str.charAt(i) - '0']++;}return zerosOnes;}
}