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

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

全局:

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

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

x
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?

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


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


上一篇:【第三轮】6.16-6.22 CareerCup 1.5
下一篇:【第三轮】6.16-6.22 CareerCup 1.7
全局:
本帖最后由 zhenzhenanan 于 2014-6-17 23:30 编辑

【解题思路】
LeetCode原题。这道题的思路没有什么特别的,就是直接旋转image。难点在于对index的处理。
【时间复杂度】
O(n^2)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/seemuch/af64b140e576b6ea73ad#file-1_6-cc
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
回复

使用道具 举报

推荐
habina 2014-6-16 16:21:15 | 只看该作者
全局:
【解题思路】
  One time transpose + one time horizontally flip = one time clockwise rotate
【时间复杂度】
  O(N^2)
【空间复杂度】
  O(1)
【gist link】
  https://gist.github.com/habina/489a964e110b4e0e254b

评分

参与人数 1大米 +1 收起 理由
ivycheung1208 + 1 good idea!

查看全部评分

回复

使用道具 举报

推荐
qianhuang 2014-6-16 10:16:26 | 只看该作者
全局:
【解题思路】
assume the matrix is rotated clockwise. Since flipping a matrix doesn't need additional space, we can use several flip to do rotation.
【时间复杂度】
O(N^2)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/qianhuang/373888abc7fa892cd949
回复

使用道具 举报

🔗
白萝卜 2014-6-15 23:38:21 | 只看该作者
全局:
被LZ的帖子刷屏....

点评

尽量不要回复无关的内容哈,这是讨论题目的帖子  发表于 2014-6-15 23:38
回复

使用道具 举报

🔗
readman 2014-6-16 23:26:44 | 只看该作者
全局:
【解题思路】
same to the one on book
【时间复杂度】
O(N^2)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/gaoyike/cd4f357ded2de159372d
回复

使用道具 举报

🔗
兰橘清檬 2014-6-17 08:27:24 | 只看该作者
全局:
【解题思路】
和书上思路一样,即 leetcode 的 Rotate Image 一题
【时间复杂度】
O(N^2)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/JoyceeLee/34ca83e5fb06ba183fe2
回复

使用道具 举报

🔗
chouclee 2014-6-18 00:40:44 | 只看该作者
全局:
【解题思路】
从图像最外圈往最里圈操作,比如[1,2,3;4,5,6;7,8,9], clockwise的话,1和3交换,3和9交换,9和7交换;然后2和6交换,6和8交换,8和4交换;最外圈完成。
一共要进行~3/4×N^2次交换
【时间复杂度】O(n^2)
【空间复杂度】O(1)
【gist link】https://gist.github.com/chouclee/38071b3c986338e9ee0c
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
***********TEST**************
1

ClockWise:
1

CounterClockWise:
1
***********TEST**************
1 2
3 4

ClockWise:
3 1
4 2

CounterClockWise:
1 2
3 4
***********TEST**************
1 2 3
4 5 6
7 8 9

ClockWise:
7 4 1
8 5 2
9 6 3

CounterClockWise:
1 2 3
4 5 6
7 8 9
***********TEST**************
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

ClockWise:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4

CounterClockWise:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
***********TEST**************
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

ClockWise:
21 16 11 6 1
22 17 12 7 2
23 18 13 8 3
24 19 14 9 4
25 20 15 10 5

CounterClockWise:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
回复

使用道具 举报

🔗
wilbert 2014-6-18 05:00:27 | 只看该作者
全局:
【解题思路】
Imaging the matrix is layered by layers of numbers in a square loop, we start from outer layer and going inside. We only look at one edge of the square loop start from the left-top corner, and calculate the relative indices for position of the rest edges. swap the four numbers with the corresponding indices. If N is odd, the inner most number does not need to be rotated, since it is the only one number in the layer.
【时间复杂度】
O(n^2)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/iwilbert/3a97e8dc2ec528a15602
回复

使用道具 举报

🔗
xjbTalk 2014-6-18 21:37:49 | 只看该作者
全局:
【解题思路】
【时间复杂度】
O(n^2)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/3c4ba6084f62f4f56c4d.git
回复

使用道具 举报

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

本版积分规则

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