网站系统开发方式,wordpress调用标题,网页设计与制作实用教程电子资源,新西兰网站后缀给定一个 m x n 的矩阵#xff0c;如果一个元素为 0 #xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 示例 1#xff1a;
输入#xff1a;matrix [[1,1,1],[1,0,1],[1,1,1]]
输出#xff1a;[[1,0,1],[0,0,0],[1,0,1]]示例 2#xff1a; 输入如果一个元素为 0 则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 示例 1
输入matrix [[1,1,1],[1,0,1],[1,1,1]]
输出[[1,0,1],[0,0,0],[1,0,1]]示例 2 输入matrix [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出[[0,0,0,0],[0,4,5,0],[0,3,1,0]]
package TOP11_20;import java.util.HashSet;
import java.util.Set;// 矩阵置零
//给定一个 m x n 的矩阵如果一个元素为 0 则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。
// 输入matrix [[1,1,1],[1,0,1],[1,1,1]]
//输出[[1,0,1],[0,0,0],[1,0,1]]
public class Top18 {// 用两个hash表记录行和列为0的数据 然后再修改数组值public static void setZeroes(int[][] matrix) {Set colZeroSets new HashSet();Set rowZeroSets new HashSet();int row matrix.length;int height matrix[0].length;for (int i 0; i row; i) {for (int j 0; j height; j) {if (matrix[i][j] 0) {colZeroSets.add(i);rowZeroSets.add(j);}}}for (int i 0; i row; i) {for (int j 0; j height; j) {if (colZeroSets.contains(i) || rowZeroSets.contains(j)) {matrix[i][i] 0;}}}}// 直接用第一行和第一列 来记录但先将第一行和第一列是否有为0的数据记录下来 然后再赋值最后赋值第一行和第一列public static void setZeroes2(int[][] matrix) {int row matrix.length;int height matrix[0].length;boolean firstRowHasZero false;boolean firstColHasZero false;for(int i0 ;irow;i){if(matrix[i][0] 0){firstRowHasZero true;break;}}for(int j0 ;jheight;j){if(matrix[0][j] 0){firstColHasZero true;break;}}for(int i 1;irow;i){for(int j1;jheight;j){if(matrix[i][j] 0){matrix[i][0] 0;matrix[0][j] 0;}}}for(int i 1;irow;i){for(int j 1;jheight;j){if(matrix[i][0] 0 || matrix[0][j] 0){matrix[i][j] 0;}}}if(firstRowHasZero) {for(int i0 ;irow;i){matrix[i][0] 0;}}if(firstColHasZero) {for(int j0 ;jheight;j){matrix[0][j] 0;}}}public static void main(String[] args) {int[][] martirc {{1,1,1},{1,0,1},{1,1,1}};setZeroes2(martirc);System.out.println(end);}
}harryptter / LeetcodeTop100 · GitCode 文章转载自: http://www.morning.xctdn.cn.gov.cn.xctdn.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.ttaes.cn.gov.cn.ttaes.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.bbtn.cn.gov.cn.bbtn.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.hongjp.com.gov.cn.hongjp.com http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.jfxth.cn.gov.cn.jfxth.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.bchgl.cn.gov.cn.bchgl.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.gfjgq.cn.gov.cn.gfjgq.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.wpsfc.cn.gov.cn.wpsfc.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn