"070-set Matrix Zeroes (matrix 0)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original Question
Given a m x n matrix, if an element was 0, set its entire row and column to 0. Do it on place.
Main Topic
Given a m*n matrix, if a location is 0. Set the corresponding row and column to 0.
Thinking of solving problems
Scan the matrix first, mark the rows and columns to be set to 0, mark the No. 0 column with the row to set 0, and the 0 column to mark on line No. 0. Two variables are also recorded whether the No. 0 and No. 0 columns are to be set to 0, and the last to place 0 operations.
Code Implementation
Algorithm implementation class
public class Solution {public void Setzeroes (int[][]Matrix) {//The first line is set to the flagBoolean rowflag = false;//The first column is set to the flagBoolean colflag = false; for(inti =0; I <Matrix. length; i++) { for(intj =0; J <Matrix[0].length; J + +) {if(MatrixI [j] = =0) {//Mark the first line to be set if(i = =0) {Rowflag = true; }//Mark the first column to be set if(J = =0) {Colflag = true; }//rows and columns in the marker to be set to 0 Matrix[0][J] =0;MatrixI [0] =0; } } }//The element setting starting at the second column of the second row 0 for(inti =1; I <Matrix. length; i++) { for(intj =1; J <Matrix[0].length; J + +) {if(MatrixI [0] ==0||Matrix[0][J] = =0) {MatrixI [j] =0; } } }//Set first behavior 0 if(Rowflag) { for(intj =0; J <Matrix[0].length; J + +) {Matrix[0][J] =0; } }//Set the first column to 0 if(Colflag) { for(inti =0; I <Matrix. length; i++) {MatrixI [0] =0; } } }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47371979"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java implementation" "070-set Matrix Zeroes (matrix 0)"