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

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

附上代码:

 void setZeroes(vector<vector<int>>& matrix) {
int n = matrix.size(), m = matrix[].size();
int *row = (int*) malloc((n / + ) * sizeof(int));
int *col = (int*) malloc((m / + ) * sizeof(int)); for(int i = ; i < n / + ; i ++) row[i] = ;
for(int i = ; i < m / + ; i ++) col[i] = ; int posInt , posBit ;
for(int i = ; i < n; i ++){
for(int j = ; j < m; j ++){
if(matrix[i][j] == ){
cout<<" i = "<<i <<" j = "<<j <<endl;
posInt = i / , posBit = i % ;
row[posInt] |= ( << posBit);
posInt = j / , posBit = j % ;
col[posInt] |= ( << posBit);
}
}
} for(int i = ; i < n; i ++){
posInt = i / ;posBit = i % ;
if(row[posInt] & ( << posBit)){
for(int j = ; j < m; j ++){
matrix[i][j] = ;
}
}
} for(int i = ; i < m; i ++){
posInt = i / ;posBit = i % ;
if(col[posInt] & ( << posBit)){
for(int j = ; j < n; j ++){
matrix[j][i] = ;
}
}
}
}

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

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

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

  1. 【LeetCode】73. Set Matrix Zeroes (2 solutions)

    Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...

  2. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

  3. 【LeetCode】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. Fo ...

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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. 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. click ...

  7. 【leetcode】867 - Transpose Matrix

    [题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...

  8. 【LeetCode】766. Toeplitz Matrix 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...

  9. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

随机推荐

  1. 1289大鱼吃小鱼(STL中栈的应用)

    >>点击进入测试<< 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向 ...

  2. FZU 1492 地震预测(模拟链表的应用)(Java实现)

    FZU 1492 地震预测(模拟链表的应用)(Java实现) 怀特先生是一名研究地震的科学家,最近他发现如果知道某一段时间内的地壳震动能量采样的最小波动值之和,可以有效地预测大地震的发生. 假设已知一 ...

  3. Java中list集合ArrayList 中contains包含的使用

    Java中list集合ArrayList 中contains包含的使用 https://blog.csdn.net/qq_38556611/article/details/78774690

  4. hdu 5017 模拟退火算法

    hdu 5017 http://blog.csdn.net/mypsq/article/details/39340601 #include <cstdio> #include <cs ...

  5. 【Eclipse】Eclipse 快捷键

    Eclipse 快捷键 关于快捷键 Eclipse 的很多操作都提供了快捷键功能,我们可以通过键盘就能很好的控制 Eclipse 各个功能: 使用快捷键关联菜单或菜单项 使用快捷键关联对话窗口或视图或 ...

  6. M - 小希的迷宫 并查集

    上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...

  7. cogs——1578. 次小生成树初级练习题

    1578. 次小生成树初级练习题 ☆   输入文件:mst2.in   输出文件:mst2.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 求严格次小生成树 [输入格式 ...

  8. ubuntu How do I configure proxies without GUI?

    想法:  我的想法是想是一台国内的 ubuntu 云主机可以通过另外一台在国外(新加坡)的服务器 ,来实现可以访问 google ,哈哈,比较好查资料:) 下面的做法 去修改 /etc/environ ...

  9. spark sql读hbase

    项目背景 spark sql读hbase据说官网如今在写,但还没稳定,所以我基于hbase-rdd这个项目进行了一个封装,当中会区分是否为2进制,假设是就在配置文件里指定为#b,如long#b,还实用 ...

  10. [BZOJ 2006] 狼抓兔子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1001 [算法] 最小割 [代码] #include<bits/stdc++.h ...