沈阳做网站优化的公司,百度seo关键词优化si,网站开发吗和APP软件一样吗,企业信息查询系统官网广东创建二维前缀和数组
两个for循环#xff0c;外循环表示子矩阵的左上角#xff08;x1,y1#xff09;#xff0c;内循环表示子矩阵的右下角#xff08;x2,y2#xff09;
两个for循环遍历#xff0c;计算子矩阵的元素总和
四个变量#xff0c;暴力破解的时间复杂度为O(…创建二维前缀和数组
两个for循环外循环表示子矩阵的左上角x1,y1内循环表示子矩阵的右下角x2,y2
两个for循环遍历计算子矩阵的元素总和
四个变量暴力破解的时间复杂度为O(m^2*n^2)m、n为matrix数组的行数和列数 优化
计算每一行的前缀和而不是整个矩阵的前缀和。
取不同的两个列(j1,j2)计算以这两个列为边界计算每一行的前缀和这就是二维前缀和
这样就可以减少一个变量遍历时间复杂度为O(m^2*n)m、n为matrix数组的行数和列数 代码
import org.junit.Test;import java.util.HashMap;
import java.util.Map;public class SubmatrixNumber {Testpublic void test() {int[][] matrix new int[][]{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}};
// for (int[] arr:getPrefixAnd(matrix)) {
// for (int i:arr) {
// System.out.print(i );
// }
// System.out.println();
// }System.out.println(submatrixNumber(matrix, 0));//4int[][] matrix1 new int[][]{{1, -1}, {-1, 1}};System.out.println(submatrixNumber(matrix1, 0));//5int[][] matrix2 new int[][]{{904}};System.out.println(submatrixNumber(matrix2, 0));//0}public static int submatrixNumber(int[][] matrix, int target) {int[][] sum getPrefixAnd1(matrix);int ans 0;MapInteger, Integer count new HashMap();//负责记录计算出的每两列之间的前缀和出现的次数int endY 1;while (endY matrix[0].length) {for (int startY 0; startY endY; startY) {//两个列的边界int prefixAnd 0;count.clear();//两个列的边界不同此时计算出的前缀和不同负责记录的map集合需要初始化count.put(0, 1);//当矩阵为空时需要记录0出现了1次for (int k 1; k matrix.length; k) {//遍历行prefixAnd (sum[k][endY] - sum[k][startY]);//计算以这两个列为边界计算每一行的前缀和if (count.containsKey(prefixAnd - target)) {//count集合中是否存在key值为temp-target的数即为temp-target这个数是否是第一次出现ans count.get(prefixAnd - target);//不是第一次则取value值加上}count.put(prefixAnd, count.getOrDefault(prefixAnd, 0) 1);//记录次数加一//defaultValue - 当指定的key并不存在映射关系中则返回的该默认值//即如果是第一次出现则默认的次数记为0}}endY;}return ans;}public static int[][] getPrefixAnd(int[][] matrix) {int[][] sum new int[matrix.length][matrix[0].length];for (int i 0; i matrix.length; i) {for (int j 0; j matrix[0].length; j) {if (i 0 j 0) {//第一行sum[i][j] sum[i][j - 1] matrix[i][j];} else if (j 0 i 0) {sum[i][j] sum[i - 1][j] matrix[i][j];} else if (i 0 j 0) {sum[0][0] matrix[0][0];} else {sum[i][j] sum[i - 1][j] sum[i][j - 1] - sum[i - 1][j - 1] matrix[i][j];}}}return sum;}//sum[0][0] 0public static int[][] getPrefixAnd1(int[][] matrix) {int[][] sum new int[matrix.length1][matrix[0].length1];for (int i 1; i matrix.length; i) {for (int j 1; j matrix[0].length; j) {sum[i][j] sum[i][j - 1] matrix[i - 1][j - 1];}}return sum;}
}
文章转载自: http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.ktxd.cn.gov.cn.ktxd.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.bfwk.cn.gov.cn.bfwk.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.lgkbn.cn.gov.cn.lgkbn.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.kysport1102.cn.gov.cn.kysport1102.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn