"Leetcode"--73. Set Matrix Zeroes

Source: Internet
Author: User

Problem Description: An element with a value of 0 in a two-dimensional array, where the row or column is full set is 0;https://leetcode.com/problems/set-matrix-zeroes/

Problem Analysis: Constant space is required in the question. Naturally think of bit optimization. An int can store information for 31 elements. Here is the use of the string in the paper is commonly used in the optimization of processing methods. Like the idea of a barrel. Well, it seems that this long paper is not a white look.

Attached code:

1 voidSetzeroes (vector<vector<int>>&matrix) {2         intn = matrix.size (), M = matrix[0].size ();3         int*row = (int*)malloc(N/ to+2) *sizeof(int));4         int*col = (int*)malloc((M/ to+2) *sizeof(int));5        7          for(inti =0; I < n/ to+2; i + +) row[i] =0;8          for(inti =0; I < m/ to+2; i + +) col[i] =0;9         Ten         intPosint, Posbit; One          for(inti =0; I < n; i + +){ A              for(intj =0; J < M; J + +){ -                 if(Matrix[i][j] = =0){ -cout<<"i ="<<i <<"j ="<<j <<Endl; thePosint = I/ to, posbit = i% to; -Row[posint] |= (1<<posbit); -Posint = J/ to, Posbit = j to; -Col[posint] |= (1<<posbit); +                 } -             } +         } A          at          for(inti =0; I < n; i + +){ -Posint = I/ to;p osbit = i% to; -             if(Row[posint] & (1<<posbit)) { -                  for(intj =0; J < M; J + +){ -MATRIX[I][J] =0; -                 } in             } -         } to          +          for(inti =0; I < m; i + +){ -Posint = I/ to;p osbit = i% to; the             if(Col[posint] & (1<<posbit)) { *                  for(intj =0; J < N; J + +){ $Matrix[j][i] =0;Panax Notoginseng                 } -             } the         } +}

Reflection: Originally a very simple question medium difficulty, but encountered a big pit, that is, I began to use the Memset function to initialize the dynamic array row and Col. Big data on the strange bug, and finally found a long time to locate this initialization function here, using the For loop manual initialization, the problem is resolved.

Memset is used to initialize a string, but since ASCII (0) = Null,null is exactly 0, it can be used to initialize the array to 0, but it is important to note that the size of the initialization (n * sizeof int) is not initialized char[]. The direct use of sizeof (array name) confusion, here *row is just a pointer to the N-int region, the size of an int is also.

"Leetcode"--73. Set Matrix Zeroes

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.