【LeetCode】-- 73. Set Matrix Zeroes

问题描述:将二维数组中值为0的元素,所在行或者列全set为0;https://leetcode.com/problems/set-matrix-zeroes/

问题分析:题中要求用 constant space 的辅助空间。自然想到位优化。一个int可以存储31个元素的信息。这里刚好用到了字符串论文里面常用的优化处理方法。类似桶分的思想。好吧,这么看来这长时间的论文没白看。

附上代码:

 1 void setZeroes(vector<vector<int>>& matrix) {
 2         int n = matrix.size(), m = matrix[0].size();
 3         int *row = (int*) malloc((n / 31 + 2) * sizeof(int));
 4         int *col = (int*) malloc((m / 31 + 2) * sizeof(int));
 5
 7         for(int i = 0; i < n / 31 + 2; i ++) row[i] = 0;
 8         for(int i = 0; i < m / 31 + 2; i ++) col[i] = 0;
 9
10         int posInt , posBit ;
11         for(int i = 0; i < n; i ++){
12             for(int j = 0; j < m; j ++){
13                 if(matrix[i][j] == 0){
14                     cout<<" i = "<<i <<"  j = "<<j <<endl;
15                     posInt = i / 31, posBit = i % 31;
16                     row[posInt] |= (1 << posBit);
17                     posInt = j / 31, posBit = j % 31;
18                     col[posInt] |= (1 << posBit);
19                 }
20             }
21         }
22
23         for(int i = 0; i < n; i ++){
24             posInt = i / 31;posBit = i % 31;
25             if(row[posInt] & (1 << posBit)){
26                 for(int j = 0; j < m; j ++){
27                     matrix[i][j] = 0;
28                 }
29             }
30         }
31
32         for(int i = 0; i < m; i ++){
33             posInt = i / 31;posBit = i % 31;
34             if(col[posInt] & (1 << posBit)){
35                 for(int j = 0; j < n; j ++){
36                     matrix[j][i] = 0;
37                 }
38             }
39         }
40     }

反思:本来是一个很简单的问题medium难度的,可是遇到了一个大坑,就是我开始使用memset函数对动态数组row和col进行初始化。大数据上出现了离奇的bug,最后找了很久才定位到这个初始化函数这里,使用for循环手动初始化,问题得到解决。

  memset 是用来初始化字符串的,但因为Ascii (0) = NULL,NULL的Ascii刚好是0,所以可以用来给数组初始化成0,但是这里要注意初始化的大小(n * sizeof int),不要被初始化char[]的时候,直接使用的sizeof(数组名)混淆,这里*row只是一个指向n个int区域的指针,其大小还是一个int的size。

时间: 2024-11-08 15:36:20

【LeetCode】-- 73. Set Matrix Zeroes的相关文章

【一天一道LeetCode】#73. Set Matrix Zeroes

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. (二)解题 题目大意:给定一个M*N的数组,如果(i,j)为0,则将第i行第j列全部元素置为0. 这道题目意思很简单,如果考虑

【leetcode】Search 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row

【LeetCode】172 - Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Solution :计算包含的2和5组成的pair的个数,因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定. 观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n/5就可以. 但是25! = 有6个5(有5个5来自

【LeetCode】542. 01 Matrix

Problem description Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1. Example 1: Input: 0 0 0 0 1 0 0 0 0 Output: 0 0 0 0 1 0 0 0 0 Example 2: Input: 0 0 0 0 1 0 1 1 1

LeetCode OJ 73. Set Matrix Zeroes

题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解答 这题太水了,根本不是medium难度,一遍就AC了. 遍历matrix,拿两个数组分别记录需要变成0的行和列就OK了. 下面是AC的代码: class Solution { public: void setZeroes(vector<vector<int>>& matrix)

【Leetcode】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【Leetcode】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio