本文主要是介绍474.一和零。01背包。详细注释版0ms,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
class Solution {public int findMaxForm(String[] strs, int m, int n) {if (strs.length == 0) return 0;int[][] dp =new int[m+1][n+1];//strs 数组里的元素就是物品,m和n是背包,二维的背包// 遍历物品,字符串中有各个字符for(String s :strs) {int zero = 0,one = 0;//统计字符串中各个字符的01数量。for(char c:s.toCharArray()) {if (c == '0') ++zero;else ++one;}// 两个都是>=,因为这两个都是遍历背包容量for (int i = m; i >=zero; i--) {for (int j = n; j >= one; j--) {dp[i][j] = Math.max(dp[i][j], 1+dp[i-zero][j-one]);}}}return dp[m][n];}
}
这篇关于474.一和零。01背包。详细注释版0ms的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!