(Daily algorithm) Leetcode--set Matrix Zeroes (matrix 0)

Source: Internet
Author: User

Given a matrix, if there are 0 elements then the rows and columns containing the 0 elements are zeroed.

given a  m  X  n  matrix, if an element was 0, set its entire row and column to 0. Do it in place.

The most intuitive solution is to open up a new matrix, when the original matrix exists 0 elements, the new matrix of the corresponding row and column to zero. Such a high degree of space complexity, but also the topic is not allowed.

The difficulty of the topic is that if you encounter the 0 element immediately after the operation on the matrix, the row and column will be placed to zero. In the next traversal, if you encounter zero again, you are not sure whether the original matrix of 0 or after you modify the 0. So, one way is to record the row by two arrays, there are no 0 elements on the column, and so on the matrix after the completion of the unified modification can be.

Class Solution {public:    void Setzeroes (Vector<vector<int> > &matrix) {      const size_t m = Matrix.size ();    Const size_t n = matrix[0].size ();    Vector<bool> row (M, false); Marks whether the row exists    with 0 vector<bool> col (n, false);        for (int i = 0; i < matrix.size (), i++)    {for    (int j = 0; J < matrix[0].size (); + j) {if (matrix[i][j] = = 0) row[ I] = col[j] = true;//If 0 exists in the original matrix, the position of the row and column is recorded. }    } for (size_t i = 0; i < m; ++i) {if (Row[i]) fill (&matrix[i][0], &matrix[i][0] + N, 0);//Set the line of the Matrix to zero}for (s ize_t j = 0; J < N; ++J) {if (Col[j]) {for (size_t i = 0; i < m; ++i) matrix[i][j] = 0;//Place a column of the matrix as 0       }}}    ;

The fill function is to give the element of an interval a Val value. Function parameters: Fill (first,last,val),//first as the container's first iterator, last as the container's end iterator, and Val as the value to be replaced.

The function is the same as the following:

Template <class ForwardIterator, class t>  void Fill (forwarditerator first, ForwardIterator last, const t& V AL) {  while (first! = last)    {*first = val;    ++first;}  }
An example of using the Fill function:

Fill algorithm example#include <iostream>     //Std::cout#include <algorithm>    //std::fill# Include <vector>       //Std::vectorint main () {  std::vector<int> myvector (8);                       myvector:0 0 0 0 0 0 0 0  std::fill (myvector.begin (), Myvector.begin () +4,5);   Myvector:5 5 5 5 0 0 0 0  std::fill (myvector.begin () +3,myvector.end () -2,8);   Myvector:5 5 5 8 8 8 0 0  std::cout << "myvector contains:";  For (Std::vector<int>::iterator It=myvector.begin (); It!=myvector.end (); ++it)    std::cout << ' < < *it;  Std::cout << ' \ n ';  return 0;}

The output is:Myvector contains:5 5 5 8 8 8 0 0

Fill_n function The function is : give you a starting point, and then give you a value of Count and Val. Assigns a value from the starting point to the Count element Val

Equivalent to the following function:

Template <class Outputiterator, class Size, class t>  outputiterator fill_n (outputiterator First, size n, const t& val) {  while (n>0) {    *first = val;    ++first; --n;  }  return first;     Since c++11}

Returns the value of the first iterator in c++11 and nothing in c++98. n cannot be a negative number in 98, in 11, if it is negative, nothing is done.

An example:

Fill_n example#include <iostream>     //Std::cout#include <algorithm>    //Std::fill_n#include < Vector>       //Std::vectorint main () {  std::vector<int> myvector (8,10);        Myvector:10  Std::fill_n (Myvector.begin (), 4,20);     MYVECTOR:20 Ten  Std::fill_n (Myvector.begin () +3,3,33);   Myvector:20  std::cout << "myvector contains:";  For (Std::vector<int>::iterator It=myvector.begin (); It!=myvector.end (); ++it)    std::cout << ' < < *it;  Std::cout << ' \ n ';  return 0;}
This also leads to the difference between pointers and iterators:

same point --- Pointers anditeratorare supported with integers+,-operation, and it means moving forward or backward from the current position.Na position;Pointers anditeratorall support subtraction operations, pointers-The pointer gets the distance between the two pointers, and the iterator-The iterator gets the distance between the two iterators;&NBSP; via pointer or Iterator

different points --- &NBSP; cout operator can directly output the value of the pointer, but the iterator will be in operation when the error. By reading the wrong message and the header file, the iterator returns the object reference instead of the value of the object, so cout only output iterators use * Value is not directly output itself;    pointer can point to function and iterator not , the iterator can only point to the container.

< Span style= "Color:rgb (51,51,51); Font-family:verdana,geneva,arial,helvetica,sans-serif; line-height:19px; text-indent:-24px "> < Span size= "3" >

< Span size= "3" > pointer is a special variable that is designed to hold the address of another variable, An iterator is simply an STL interface that is designed with reference to the characteristics of pointers.

< Span size= "3" >< Span style= "White-space:pre" > there is such a statement on the Web: iterators are generalized pointers, The pointer satisfies all of the iterator requirements. Iterators are interfaces to STL algorithms, and pointers are iterators, so the STL algorithm can use pointers to manipulate pointers-based non-STL containers.

< Span size= "3" > maybe some compilers use pointers to implement iterators so that some people mistakenly think pointers and iterators are a concept.

The iterator is not a normal pointer, it can be said to be a smart pointer. Only the iterator is the normal pointer in the vector container. In other cases iterator are more complex data than enough. The iterator is divided into five different types: read-only, write-only, forward, bidirectional, and random access. Depending on the container and the use case, the iterator uses different types. Like List is two-way iterator. All iterator are in accordance with the principle of left closed right opening.

< Span size= "3" >




(Daily algorithm) Leetcode--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.