由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Set Matrix Zeroes const space solution
相关主题
leetcode中那道Set Matrix Zeroes怎么做Google面试问题
一个startup公司的面试题今天topcoder上一道漂亮的题目
matrix question问个careerup的题
set matrix zero最少用多少space?问一道题
Factorial Trailing Zeroes这道题为什么用pow(5,k)而不是 f*=5;?请教一道yahoo的system design的题目
问个google面试题bloomberg面经
Search a 2D Matrix的两种写法,哪种更好?bloomberg on-campus interview, 攒rp,求祝福
combination sum2的问题find subset that sum up to given number
相关话题的讨论汇总
话题: row话题: col话题: mat话题: thecol话题: set
进入JobHunting版参与讨论
1 (共1页)
S******n
发帖数: 132
1
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?
谁给点提示,一下想不出来
n********r
发帖数: 102
2
用搜到的第一个为0的row和col,存后面为0的点的坐标
然后根据这个row和col里的0来set zeros
S******n
发帖数: 132
3
不是太明白,能不能稍微说详细点?
s*******s
发帖数: 1031
4
我是这样做的
第一遍:找到整个matrix的最大值,然后把所有的0编程最大值+1,
第二遍:
对所有最大值+1的元素,将行列变成0
第三编:
把所有最大值+1的元素变成0
O(n),memory: O(1)

0

【在 S******n 的大作中提到】
: 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?
: 谁给点提示,一下想不出来

r*****e
发帖数: 792
5
和下面说的思想一样,但是要注意一点,code有注释,否则所有entry都变成0了。
for (row=0; row for (col=0; col if (mat[row][col]==0) {
if (!foundZero) {
foundZero=true;
therow=row;
thecol=col;
} else {
mat[therow][col]=0;
mat[row][thecol]=0;
}
}
}
}
if (!foundZero) return;//do nothing
for (col=0; col if (mat[therow][col]==0 && col!=thecol) {//col!=thecol is meant to set
for thecol at last to avoid setting all elements to 0
for (row=0; row mat[row][col]=0;
}
}
for (row=0; row for (col=0; col if (mat[row][thecol]==0) mat[row][col]=0;
}
}
/* do this at last, o/w all elements will be set to 0 */
for (row=0; row mat[row][thecol]=0;

【在 n********r 的大作中提到】
: 用搜到的第一个为0的row和col,存后面为0的点的坐标
: 然后根据这个row和col里的0来set zeros

1 (共1页)
进入JobHunting版参与讨论
相关主题
find subset that sum up to given numberFactorial Trailing Zeroes这道题为什么用pow(5,k)而不是 f*=5;?
贡献一道面试题问个google面试题
Quantcast这个公司怎么样呀?Search a 2D Matrix的两种写法,哪种更好?
申请OPT时最后一个学期是否必须要注册full-time?combination sum2的问题
leetcode中那道Set Matrix Zeroes怎么做Google面试问题
一个startup公司的面试题今天topcoder上一道漂亮的题目
matrix question问个careerup的题
set matrix zero最少用多少space?问一道题
相关话题的讨论汇总
话题: row话题: col话题: mat话题: thecol话题: set