Matrix 0--Enter Silicon Valley

Source: Internet
Author: User

Given a two-dimensional m*n matrix, if an element is 0, then all elements of its row and column are set to 0. Additional space is not allowed.

Just see this problem, only think of open two arrays, one to record the row to be set to 0, one to record the column to be set to 0, but this need to use extra space. The solution in the book, using an existing array, first find a bit 0 element, record the row and column location, and then in the subsequent scanning process, if an element is 0, then the first 0 row and the column position of this element is set to 0, the first column of 0 and the position of this element's row is set to 0, the second scan , skipping the first row with the column element, if an element in the first 0 element of the row with this element of the column or the first 0 element of the column with this element's row position is 0, set this element to 0, and finally the row and column of the first 0 element are all 0.

Matrix 0--Enter Silicon Valley

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.