Leetcode "Set Matrix zeroes" Python implementation

Source: Internet
Author: User

title :

Given a m x n Matrix, if an element was 0, set its entire row and column to 0. Do it on 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 is not the best solution.
Could you devise a constant space solution?

Code: OJ tested via runtime:220 MS
1 classSolution:2     #@param Matrix, a list of lists of integers3     #RETURN Nothing, MODIFY matrix in place.4     defsetzeroes (self, matrix):5         #None case6         ifMatrix isNone:7             returnNone8         #dimension of the matrix9ROW =len (Matrix)TenCOL =Len (matrix[0]) One         #record the status of first row and first column AFirst_row_contains_zero =False -          forIinchRange (COL): -             ifMatrix[0][i] = =0: theFirst_row_contains_zero =True -First_col_contains_zero =False -          forIinchRange (ROW): -             ifMatrix[i][0] = =0: +First_col_contains_zero =True -         #visit the row-1xcol-1 matrix and check zeros +          forRowinchRange (1, ROW): A              forColinchRange (1, COL): at                 ifMatrix[row][col] = =0: -Matrix[row][0] =0 -Matrix[0][col] =0 -         #set zero rows and zero columns -          forRowinchRange (1, ROW): -              forColinchRange (1, COL): in                 ifMatrix[row][0]==0ormatrix[0][col]==0: -Matrix[row][col] =0 to         ifFirst_row_contains_zero: +              forIinchRange (COL): -Matrix[0][i] =0 the         ifFirst_col_contains_zero: *              forIinchRange (ROW): $Matrix[i][0] = 0

Ideas :

The difficulty of the topic is to require the use of constant space.

The method is to first record whether the first and first rows of the matrix contain 0 elements, and then use the first and first columns as the remaining columns and rows to contain 0 of the record positions, thus saving O (m+n) space.

Here's a stem to be explained:

For example Matrix[0][j] as the Matrix J column contains 0 of the flag, there are two cases:

1. Matrix[0][j] itself is 0, the elements of the entire J column (including Matrix[0][j]) are finally 0

2. Matrix[0][j] is nonzero if the remaining elements of J column have 0, then Matrix[0][j] is 0, and if the remaining elements of J column do not contain 0, then Matrix[0][j] remains the original value and is not zero.

After the same trick, you can write the code out of AC.

Learn the following logs mainly:

Http://blog.sina.com.cn/s/blog_45b8132001018xie.html

In addition, incompatible, the first version of the code to write their own also attached.

OJ Test via runtime:213 ms

classSolution:#@param Matrix, a list of lists of integers    #RETURN Nothing, MODIFY matrix in place.    defsetzeroes (self, matrix):#None case        ifMatrix isNone:returnNone#dimension of the matrixROW =len (matrix) COL=Len (matrix[0])#record zero rows and zero columnsZero_row = [False forIinchrange (ROW)] Zero_col= [False forIinchrange (COL)] forRowinchRange (ROW): forAo.inchRange (COL):ifMatrix[row][col] = =0:zero_row[row]=True Zero_col[col]=True#set zero rows and zero columns         forRowinchRange (ROW): forColinchRange (COL):ifZero_row[row]orZero_col[col]: Matrix[row][col]= 0

It feels like the two kinds of code are almost as efficient. It's just a matter of time and space swaps.

However, since only a small sacrifice of time, there is some space, is also good, in the future can be remembered in the work of this trick.

Leetcode "Set Matrix zeroes" Python implementation

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.