(Daily algorithm) LeetCode -- Set Matrix Zeroes (Matrix Set to zero)

Source: Internet
Author: User

(Daily algorithm) LeetCode -- Set Matrix Zeroes (Matrix Set to zero)

Given a matrix, if there is a zero element, the row and column of the zero element are set to zero.

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

The most intuitive solution is to create a new matrix. When the original matrix has zero elements, the corresponding rows and columns of the new matrix are set to zero. In this way, the space complexity is high and the question is not allowed.

The difficulty of the question is that if zero elements are encountered, the operation will be performed on the matrix immediately, and the row and column will be set to zero. In the next traversal, if you encounter zero again, you do not know whether it is the zero of the original matrix or the zero after modification. Therefore, one method isFirst, two arrays are used to record whether there are zero elements in the column of the row. After traversing the matrix, you can modify it in a unified manner..

Class Solution {public: void setZeroes (vector
 
  
> & Matrix) {const size_t m = matrix. size (); const size_t n = matrix [0]. size (); vector
  
   
Row (m, false); // mark whether the row has 0 vector
   
    
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 there is 0 in the original matrix, record the position of the row and column .} } For (size_t I = 0; I <m; ++ I) {if (row [I]) fill (& matrix [I] [0], & matrix [I] [0] + n, 0); // set the row of the matrix to zero} for (size_t j = 0; j <n; ++ j) {if (col [j]) {for (size_t I = 0; I <m; ++ I) matrix [I] [j] = 0; // set a column of the Matrix to 0 }}}};
   
  
 

Fill function The role is: Assign val values to all the elements in a range. Function parameter: fill (first, last, val); // first is the first iterator of the container, last is the last iterator of the container, and val is the value to be replaced.

Functions are the same as the following functions:

template 
 
    void fill (ForwardIterator first, ForwardIterator last, const T& val){  while (first != last) {    *first = val;    ++first;  }}
 
An example of using the fill function:

// fill algorithm example#include 
 
       // std::cout#include     // std::fill#include 
  
          // std::vectorint main () {  std::vector
   
     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
    
     ::iterator it=myvector.begin(); it!=myvector.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}
    
   
  
 

Output: myvector contains: 5 5 5 8 8 8 0 0

Fill_n FunctionThe role is: Give you a starting point, and then give you a value count and val. Grant the values of the count element val from the start point in sequence.

It is equivalent to the following function:

template 
 
    OutputIterator fill_n (OutputIterator first, Size n, const T& val){  while (n>0) {    *first = val;    ++first; --n;  }  return first;     // since C++11}
 

Return the value of the first iterator in C ++ 11, and return nothing in c ++ 98. N cannot be a negative number in 98. In 11, if n is a negative number, nothing is done.

Example:

// fill_n example#include 
 
       // std::cout#include     // std::fill_n#include 
  
          // std::vectorint main () {  std::vector
   
     myvector (8,10);        // myvector: 10 10 10 10 10 10 10 10  std::fill_n (myvector.begin(),4,20);     // myvector: 20 20 20 20 10 10 10 10  std::fill_n (myvector.begin()+3,3,33);   // myvector: 20 20 20 33 33 33 10 10  std::cout << "myvector contains:";  for (std::vector
    
     ::iterator it=myvector.begin(); it!=myvector.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}
    
   
  
 
This leads to the differences between pointers and iterators:

Similarities-both the pointer and iterator support the + and-operations with integers, And the meaning is to move n positions forward or backward from the current position. Both the pointer and iterator support the subtraction operation, pointer-the pointer gets the distance between two pointers, and the iterator gets the distance between two iterators. the pointer or iterator can modify the elements it points.

Difference --- the cout operator can directly output the pointer value, but an error will be reported when performing operations on the iterator. According to the error message and header file, the iterator returns the object reference instead of the object value. Therefore, cout can only output the * value after the iterator, rather than its own value; pointers can point to functions, but iterators cannot. iterators can only point to containers.


A pointer is a special variable used to store the address of another variable. An iterator is only a STL interface designed based on the pointer feature.

There is a saying on the Internet that the iterator is a generalized pointer, And the pointer meets all the requirements of the iterator. The iterator is the interface of STL algorithm, and the pointer is the iterator. Therefore, STL algorithm can use pointers to operate non-STL containers based on pointers.

Some compilers may use pointers to implement iterators so that some may mistakenly think that pointers and iterators are a concept.

The iterator is not a common pointer, but a smart pointer. Only Iterator in a vector container is a normal pointer. In other cases, Iterator is complicated enough data. Iterator is divided into five different types: Read-only, write-only, forward, bidirectional, and random access. Iterator uses different types based on the container and usage. Like List is a two-way Iterator. All Iterator must be left closed and right open.





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.