"Leetcode-Interview algorithm classic-java implementation" "070-set Matrix Zeroes (matrix 0)"

Source: Internet
Author: User

"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)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.