[Leetcode] set matrix zeroes solution report

Source: Internet
Author: User

Today, I saw the csdn blog medal changed the chart and added the blog grade. It looks fresh and feels good!

[Question]

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Click to show follow up.

Follow up:

Did you use extra space?
A straight forward solution using O (Mn) space is probably a bad idea.
A simple improvement Uses O (m + n) space, but still not the best solution.
Cocould you devise a constant space solution?

[Idea]

After reading the following requirements, I first thought of the space of O (m + n), that is, adding a row and a column to mark which row and column have 0; instead, I don't know how to use the space of O (Mn.

Next I thought about the space of O (1). I thought about it a little and thought of the answer, that is, compared to the space of O (m + n), no additional row or column is added, the first row and the first column are used to store which row and which column has 0. Of course, it is troublesome to save the original information in the first column of the first row.

During the first two writes, because the first column of the First row is not properly processed, WA is caused.

After reading the answer from the Internet, I am very happy to see other people.

[Java code, O (1) Space]

Public class solution {public void setzeroes (INT [] [] matrix) {int M = matrix. length; int n = matrix [0]. length; int I, j; // indicates whether the first row and the first column have 0 Boolean firstrow = false, firstcol = false; For (j = 0; j <N; j ++) {If (0 = matrix [0] [J]) {firstrow = true; break ;}} for (I = 0; I <m; I ++) {If (0 = matrix [I] [0]) {firstcol = true; break ;}// traverse the second column of the Second row, if 0 is encountered, set the first value of the row and column to 0 for (I = 1; I <m; I ++) {for (j = 1; j <n; j ++) {If (0 = matrix [I] [J]) {matrix [I] [0] = 0; matrix [0] [J] = 0 ;}}// set the row 0 in the first column to 0, set the column where the first row 0 is located to 0 for (I = 1; I <m; I ++) {If (0 = matrix [I] [0]) {for (j = 1; j <n; j ++) {matrix [I] [J] = 0 ;}}for (j = 1; j <N; j ++) {If (0 = matrix [0] [J]) {for (I = 1; I <m; I ++) {matrix [I] [J] = 0 ;}}// determines whether the first row and the first column are set to 0 if (firstrow) {for (j = 0; j <n; j ++) {matrix [0] [J] = 0 ;}} if (firstcol) {for (I = 0; I <m; I ++) {matrix [I] [0] = 0 ;}}}}


[Leetcode] set matrix zeroes solution report

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.