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

[CareerCup] [第二轮] 2/18-2/24 CareerCup 1.6

全局:

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

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

x
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?

发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

评分

参与人数 1大米 +10 收起 理由
Kimurate + 10

查看全部评分


上一篇:[第二轮] 2/18-2/24 CareerCup 1.5
下一篇:[第二轮] 2/18-2/24 CareerCup 1.7
🔗
ototsuyume 2013-2-17 22:17:09 | 只看该作者
全局:
暂时没时间写长的,先用python偷懒写一种简短的解法上来
#a is image data,for example [[1,2,3],[4,5,6],[7,8,9]],d indicate the direction you want to rotate the image to,d=0 is right,other is left
def rotate(a,d):
    if d==0:
        return [[x[i] for x in a[::-1]] for i in range(len(a))]
    else:
        return [[x[i] for x in a] for i in range(len(a),-1,-1)]
回复

使用道具 举报

🔗
zuohr 2013-2-18 01:44:28 | 只看该作者
全局:
本帖最后由 zuohr 于 2013-2-18 10:14 编辑

rotate to left by 90 degrees in two steps :
1. flip vertically
2. transpose the matrix (swap m [row][col] and m[col][row])
Time complexity O(N), N is the number of pixel.

https://gist.github.com/Zuohr/4972470

回复

使用道具 举报

🔗
grassgigi 2013-2-18 06:43:44 | 只看该作者
全局:
rotate by layer, loop from outside edges to inside ones

https://gist.github.com/chrislukkk/4973845
回复

使用道具 举报

🔗
ryancooper 2013-2-18 06:56:18 | 只看该作者
全局:
zuohr 发表于 2013-2-18 01:44
rotate to left by 90 degrees in two steps :
1. flip vertically
2. tranpose the matrix (swap m [row ...

Nice job! In your solution, you decompose rotation into two simpler operation on matrix: flip and transposition. And your algorithm gives us a 90 degree rotation in counterclockwise direction. My algorithm is similar to you, but in clockwise direction
回复

使用道具 举报

🔗
ryancooper 2013-2-18 07:42:10 | 只看该作者
全局:
grassgigi 发表于 2013-2-18 06:43
rotate by layer, loop from outside edges to inside ones

https://gist.github.com/chrislukkk/497384 ...

Wow, another cool idea!Your algorithm will run faster than mine and zuohr's on large input since you only need to run 1 nested loop. But maybe for someone it seems a little hard to comprehend. when implementing, we should pay more attention to the swap order. But still, it is a nice algorithm
回复

使用道具 举报

🔗
cqx83 2013-2-18 08:01:53 | 只看该作者
全局:
https://gist.github.com/cqx83/4974220
矩阵中相应的4个位置,a[i][j],a[j][n-i-1],a[n-i-1][n-j-1],a[n-j-1][i]swap一次,矩阵中每个元素都被traverse一次,O(N^2),N为每行的元素数
回复

使用道具 举报

🔗
mazl123321 2013-2-18 08:46:04 | 只看该作者
全局:
本帖最后由 mazl123321 于 2013-2-18 11:35 编辑

by doing the swap with :
matx[ i][i][j]-> temp,
matx[n-j-1][i] ->matx[i][j],
matx[n-i-1][n-j-1] ->matx[n-j-1][i],
matx[j][n-i-1] -> matx[n-i-1][n-j-1],
temp -> matx[j][n-i-1]
https://gist.github.com/mazl123321/4974405
[/i][/i][/i][/i]
回复

使用道具 举报

全局:
clockwise direction , switch from outside to inside
https://gist.github.com/calmcaicai/4983593
回复

使用道具 举报

🔗
Arthur2012 2013-2-19 15:19:55 | 只看该作者
全局:
At first turn 180 degree, and then turn through back-diagonal.
https://github.com/Tedatworking/ ... ster/20130217_6.cpp
回复

使用道具 举报

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

本版积分规则

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