Leetcode:set Matrix Zeroes-zero all rows and columns containing 0 of the matrix

Source: Internet
Author: User

1. Title

Set matrix zeroes (The matrix contains 0 rows and columns all placed 0)

2. Address of the topic

https://leetcode.com/problems/set-matrix-zeroes/

3. Topic content

English: Given a m x n Matrix, if an element is 0, set it entire row and column to 0.

Chinese: Given a matrix of MXN, if one of the elements is 0, then the entire row or collation containing the element is set to 0

4. Method of solving Problems 1

Use the first and first columns to record whether a row or column should be set to 0, and then use two status bits to mark whether the first row and the first column should also be set to 0.

The Java code is as follows:

/** * @ function Description: leetcode 73 - set matrix zeroes * @ Developer: Tsybius2014  * @ Development Time: November 7, 2015  */public class Solution {         /**     *  given a matrix, there will be 0 rows and columns of all elements 0     *   @param  matrix     */    public void  Setzeroes (Int[][] matrix)  {                 if  (matrix == null | |  matrix.length == 0 | |  matrix[0].length == 0)  {             return;        }                 int m = matrix.length;         int n = matrix[0].length;                 boolean needFillFirstRow = false;         boolean needFillFirstColumn = false;                 //find the line number and column number that contains 0, in the first element of the row where the element is located          for  (int i = 0; i < m; i++)  {             for  (int j = 0; j  < n; j++)  {                 if  (matrix[i][j] == 0)  {                     if  (i == 0)   {                         //if the first element of a row is originally 0, record this                          needFillFirstRow = true;                      }                     if  (j == 0)  {                         //if the first element of a column is originally a 0, Record this point                          needFillFirstColumn = true;                     }                     matrix[i][0] = 0;                      matrix[0][j] = 0;                 }            }         }        //fills the rows by the first element's record          for  (int j = 1; j < n; j+ +)  {            if  (matrix[0][j] ==  0)  {                 for  (int i = 0; i < m; i++)  {                     matrix[i][j] = 0;                 }             }        }         //fills each column with the record of the first element         for  (int  i = 1; i < m; i++)  {             if  (matrix[i][0] == 0)  {                 for  (int j = 1; j  < n; j++)  {                     matrix[i][j] = 0;                 }            }         }        //confirm if you need to fill the first line          if  (Needfillfirstrow)  {             for  (int j = 0; j < n; j+ +)  {                 matrix[0][j] = 0;            }         }        //confirm if you need to populate the first column          if  (Needfillfirstcolumn)  {             for  (int i = 0; i < m; i++)  {                 matrix[i][0] = 0;             }         }    }}

5. Method of solving Problems 2

Another way is to not use elements inside the matrix to record these changes. Instead, use the list (LinkedList) or hash set (HashSet) to record those rows and columns that need to be placed 0.

Java code using LinkedList is as follows:

import java.util.linkedlist;/** * @ function Description:leetcode 73 - set matrix  zeroes * @ Developer: tsybius2014 * @ Development Date: November 7, 2015  */public class Solution  {        /**     *  given a matrix, there will be 0 rows and columns of all elements placed 0      *  @param  matrix     */     public void setzeroes (Int[][] matrix)  {                 if  (matrix == null | |  matrix.length == 0 | |  matrix[0].length == 0)  {             return;        }                 //linked list for storing line numbers and column numbers         &nBsp Linkedlist<integer> linkedlistrows = new linkedlist<integer> ();         LinkedList<Integer> linkedListColumns = new  Linkedlist<integer> ();                 int m = matrix.length;        int  n = matrix[0].length;                 //find the line number and column number containing 0         for  (int i  = 0; i < m; i++)  {             for  (int j = 0; j < n; j++)  {                 if  (matrix[i][j ] == 0)  {                     Linkedlistrows.add (i);                     linkedlistcolumns.add (j);                 }             }        }                 //will contain 0 of each row of the 0        for   (int i : linkedlistrows)  {             for  (int num = 0; num < n; num++)  {                 matrix[i][num]  = 0;            }        }                 //will contain 0 of each column 0         for  (int j : linkedlistcolumns)  {             for  (int num = 0;  num < m; num++)  {                 matrix[num][j] = 0;             }        }    }}

The Java code for

Using HashSet is as follows:

import java.util.hashset;/** * @ function Description: leetcode 73 - set matrix zeroes  * @ Developer: tsybius2014 * @ Development Date: November 7, 2015  */public class solution {         /**     *  given a matrix, there will be 0 rows and columns of all elements placed 0      *  @param  matrix     */     public void setzeroes (Int[][] matrix)  {                 if  (matrix == null | |  matrix.length == 0 | |  matrix[0].length == 0)  {             return;        }                 //linked list for storing line numbers and column numbers           Hashset<integer> hashsetrows = new hashset<integer> ();         HashSet<Integer> hashSetColumns = new HashSet<Integer> ();                 int m  = matrix.length;        int n = matrix[0]. length;                // Find the line number and column number containing 0         for  (int i = 0; i  < m; i++)  {            for  (int j = 0; j < n; j++)  {                 if  (matrix[i][j] == 0)  {      &nBsp;              hashsetrows.add (i);                      hashsetcolumns.add (j);                 }            }         }                 //will contain 0 of each row 0        for  (int i :  hashsetrows)  {            for  (int  num = 0; num < n; num++)  {                 matrix[i][num] = 0;             }        }                 //will contain 0 of each column placed 0         for  (int j : hashsetcolumns)  {             for  (int num = 0; num < m; num+ +)  {                 matrix[num][j] = 0;            }         }    }}

END

Leetcode:set Matrix Zeroes-zero all rows and columns containing 0 of the matrix

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.