问题描述:将二维数组中值为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. 【Codeforces 1009D】Relatively Prime Graph

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 1000以内就有非常多组互质的数了(超过1e5) 所以,直接暴力就行...很快就找完了 (另外一开始头n-1条边找1和2,3...n就好 [代 ...

  2. vue.js组件之间通讯的数据双向绑定----父亲把数据传递给儿子,儿子更改数据后,重新发送给父亲,父亲数据更改后,属性会重新发送个儿子,儿子刷新新数据

    vue组件是相互独立的,如果要交互通讯,这时候,就需要组件之间数据互通了 往常我们讲的都是数据传递,子传父,父传子,都没有说子和父,父与子之间的数据互通 父亲传递给儿子数据,儿子触发一个父亲方法,将最 ...

  3. T1075 明明的随机数 codevs

    http://codevs.cn/problem/1075/ 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description 明明想在学校中 ...

  4. Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率

    安装: sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor sudo apt-get update sudo apt-get in ...

  5. java实现WebService 以及客户端不同的调用方式

    java 实现WebService 以及不同的调用方式 webservice:    就是应用程序之间跨语言的调用    wwww.webxml.com.cn    1.xml    2.    ws ...

  6. Swift: 转换NSString to String

    如下代码获取一个String?的结果 let s = NSString(data: data, encoding: encoding) return s as? String

  7. How can I add files to a Jar file? (or add a file to a zip archive)

    https://stackoverflow.com/questions/12239764/how-can-i-add-files-to-a-jar-file M.java class M{ publi ...

  8. ORA-01925:maximum of 80 enabled roles exceeded

    ORA-01925:maximum of 80 enabled roles exceeded max_enabled_roles 9i的參数,10g及以后都不用了. 指定用户session的最大ena ...

  9. CoreData使用方法三: NSPredicate在CoreData中的使用

    NSPredicate在CoreData中经常使用作查询使用,相当于sql语句中的where查询子句. 最经常使用的方法为: NSPredicate *ca = [NSPredicate predic ...

  10. 喷水装置(一)(南阳oj6)(简单贪心)

    喷水装置(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 现有一块草坪,长为20米.宽为2米.要在横中心线上放置半径为Ri的喷水装置.每一个喷水装置的效果都会让 ...