查看: 5783| 回复: 47
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.16-6.22 CareerCup 1.7

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
1.7  Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。


上一篇:【第三轮】6.16-6.22 CareerCup 1.6
下一篇:【第三轮】6.23-6.29 CareerCup 1.8
全局:
本帖最后由 zhenzhenanan 于 2014-6-18 00:41 编辑

【解题思路】
LeetCode原题。
1. 设两个bool变量,一个用来描述第行是否有零;另一个用来描述第一列是否有零。
2. 遍历整个matrix,如果某一个元素是0,则把它所在的行的第一个元素设为0,并把它所在的列的第一个元素设为0。
3. 根据第一行和第一列里面的0,把对应的行和列都设为0。
4. 根据第一步中的两个变量,把第一行和第一列设为0。
【时间复杂度】
O(mn)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/seemuch/af64b140e576b6ea73ad#file-1_7-cc
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】

点评

This is the best solution for better space complexity  发表于 2014-6-18 08:25
回复

使用道具 举报

全局:
【解题思路】两次遍历,第一次记录零所在的行列,第二次置零
【时间复杂度】O(MN)
【空间复杂度】O(M+N)
【gist link】https://gist.github.com/weazord/56924e3c39d5b898251



Test 1
Original:
1        0       
0        1       

Zeroes Set:
0        0       
0        0       

Test 2
Original
1        2        0        4        5       
6        7        8        9        10       
11        12        13        14        15       
16        17        18        19        20       

Zeroes Set:
0        0        0        0        0       
6        7        0        9        10       
11        12        0        14        15       
16        17        0        19        20       

Test 3
Original
1        2        3        4        5        6       
7        8        0        10        11        12       
13        14        15        16        0        18       
19        20        21        22        23        24       
25        26        27        28        29        30       

Zeroes Set:
1        2        0        4        0        6       
0        0        0        0        0        0       
0        0        0        0        0        0       
19        20        0        22        0        24       
25        26        0        28        0        30       



回复

使用道具 举报

推荐
jaly50 2014-6-23 23:18:50 | 只看该作者
全局:
【解题思路】
     if a[i,j]=0; a[i,0]=0; a[0,j]=0;
   然后
     i>0, j>0时:再遍历a[i,0]和a[0,j],如果a[..]=0,那么设该行或该列为0
    i=0(j=0)时,check一下是本来就是有零,还是后面设的~如果是前者的话,将该行(or 列)设为全零
【时间复杂度】o(mn)
【空间复杂度】o(m+N)....其实我不懂为啥是这个复杂度...
【gist link】
https://gist.github.com/jaly50/9a2ec0ca4d7d4f31eabf
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
Original matrix is:
  1  4  4
  2  0  8
  1  3  2
  0 21 32

Set zero of the entire row and column:
  0  0  4
  0  0  0
  0  0  2
  0  0  0

点评

你的空间复杂度应该是O(1),因为你只多用了两个boolean变量,然后用矩阵本身存了结果(第一行第一列)。我是另外用了m+n个boolean存储查找的结果。BTW,邻居啊,一起加油。。。  发表于 2014-6-24 01:22
回复

使用道具 举报

🔗
qianhuang 2014-6-16 10:21:05 | 只看该作者
全局:
【解题思路】
Assume this problem want us to set 0 according the initial matrix.
Simplest method: create a MxN matrix to store where 0 appear, then according it, we can set the row and column. Additional space is O(MN), runtime is O(MN), since we must scan the whole matrix, we cannot optimize the runtime.
To optimize the space, the aim is to set row and column, so we only need to store whether there are 0 in the row/column, thus the space is O(M+N).
【时间复杂度】
O(MN)
【空间复杂度】
O(M+N)
【gist link】
https://gist.github.com/qianhuang/b93ec80f0ad06cfeb0fc
回复

使用道具 举报

🔗
habina 2014-6-16 17:06:58 | 只看该作者
全局:
【解题思路】
  Collect all indices that elements are zero
  Set element to zero where the element's horizontal or vertical index appears in the collection
【时间复杂度】
  O(NM)
【空间复杂度】
  O(1)
【gist link】
  https://gist.github.com/habina/7cab9ec45a5bf766cce3
回复

使用道具 举报

🔗
readman 2014-6-16 23:34:14 | 只看该作者
全局:

【解题思路】
same to the one on book
【时间复杂度】
N^2
【空间复杂度】
N
【gist link】
https://gist.github.com/gaoyike/bf57c6e80674c04d44b2
回复

使用道具 举报

🔗
兰橘清檬 2014-6-17 11:56:59 | 只看该作者
全局:
【解题思路】
same to the one on book
【时间复杂度】
O(mn)
【空间复杂度】
O(m+n)
【gist link】
https://gist.github.com/JoyceeLee/19a09295137e1b537987
回复

使用道具 举报

🔗
wilbert 2014-6-18 08:21:17 | 只看该作者
全局:
【解题思路】
two boolean arrays, one to tag the row which has 0s, and another to tag the column which has 0s. second pass is to set matrix[j] = 0 if row or column has zeros.
【时间复杂度】
O(M*N)
【空间复杂度】
O(M+N)
【gist link】
https://gist.github.com/iwilbert/9a6463330ddf8ac34b61
回复

使用道具 举报

🔗
chouclee 2014-6-18 11:42:52 | 只看该作者
全局:
【解题思路】Use two extra boolean arrays to record which rows and cols should be set to 0.
【时间复杂度】O(MN)
【空间复杂度】O(M+N)
【gist link】https://gist.github.com/chouclee/1ba6e45c932413c8b401
回复

使用道具 举报

🔗
jing0328 2014-6-18 22:53:25 | 只看该作者
全局:
【解题思路】find rows and columns where zeros lie, then set zeros to corresponding rows and columns
【时间复杂度】O(mn)
【空间复杂度】O(n)
【gist link】https://gist.github.com/startupjing/06c91feafb652793cfe8
回复

使用道具 举报

🔗
xjbTalk 2014-6-19 00:55:08 | 只看该作者
全局:
【解题思路】Same to previous posters.
【时间复杂度】O(n^2)
【空间复杂度】O(n)
【gist link】https://gist.github.com/849a31e739c234a7555e.git
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表