【LeetCode】-- 73. Set Matrix Zeroes
问题描述:将二维数组中值为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的更多相关文章
- 【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 ...
- 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...
- 【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 ...
- 【一天一道LeetCode】#73. Set Matrix Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 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 ...
- 【leetcode】867 - Transpose Matrix
[题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...
- 【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
随机推荐
- tyvj3737 逐个击破
描述 三大战役的平津战场上,傅作义集团在以北平.天津为中心,东起唐山西至张家口的铁路线上摆起子一字长蛇阵,并企图在溃败时从海上南逃或向西逃窜.为了就地歼敌不让其逃走,mzd制定了先切断敌人东洒两头退路 ...
- codevs3730 无线网络发射选址
题目描述 Description 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129条东西向街道和129条南北向街道所形 ...
- RxJava如何结合观察者与链式处理
RxJava如何结合观察者与链式处理 Author: Dorae Date: 2018年12月3日17:10:31 转载请注明出处 一.概述 首先问自己几个问题,如果非常清楚这几个问题的目的与答案,那 ...
- android保存bitmap到sdcard
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //判断sdcard是否存在和是否具有读写 ...
- 《Linux内核分析》MOOC课程
http://www.cnblogs.com/wickedpriest/p/4315189.html
- spring mvc日期转换(前端到后端,后端到前端)
在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...
- 用 Arduino Uno 给 Arduino Mini(Pro)烧录程序
用 Arduino Uno 给 Arduino Mini(Pro)烧录程序 准备 Arduino Uno Arduino Mini(Pro) 杜邦线若干 接线 首先去掉 Arduino 上的芯片ATM ...
- linux高级技巧:集群之keepalived
1.keepalived简单介绍 Keepalived是一个基于VRRP协议来实现的WEB服务高可用方案.能够利用其来避免单点故障.使用多台节点安装keepalived. 其它的节点用 ...
- 假设写一段代码引导PC开机这段代码是 ? Here is a tiny "OS" :-D
Hello world -- OS 我找到了华科绍志远博士的相关代码,发现他依据MIT的JOS的boot.S 稍作改动.然后单独剥离出来,能够非常好玩~ 资料下载地址: http://download ...
- [dfs] UVALive 3667 Ruler
题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668">https://ic ...