Chapter 1 Arrays and Strings - 1.6

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

The question is not clear. The interviewer should specify by which direction the matrix should be rotated (clockwise or counter-clockwise). I will rotate it clockwise by 90 degrees.

Do it by copying is quite easy:
def rotate_90degree(M, N):
    M_new = [[0 for j in range(0, N)] for i in range(0, N)]
    for i in range(0, N):
        for j in range(0, N):
            M_new[j][N-i-1] = M[i][j]
    return M_new

For the in place requirement, I came up with a smart solution quickly (by drawing some instances on the paper). In my solution, I divide a matrix into several circles (rectangles) and each circle can rotate independently. The rotation of a circle can be simplified to rotation of several pixels on this circle and the rotation of other pixels will be driven by rotation of these pixels. So I successfully reduce the problem to rotation of one pixel in place, which seems easy enough.

My solution is similar to the standard one. Dividing the problem into sub problems is a great strategy!
def rotate_90degree_inplace(M, N):
    last_circle = 0
    if N%2 == 0:
        last_circle = int(N/2)
    else:
        last_circle = int(N/2) - 1
    
    # From outermost to center, rotate each circle of elements
    for i in range(0, last_circle):
        rotate_circle(i, M, N)


def rotate_circle(i, M, N):
    # Rotate each element on the circle.
    # Only a part of the elements on the circle should be
    # rotated and the rotation of other elements will be driven by them
    for j in range(i, N-i-1):
        rotate_pixel(i, j, M, N)


def rotate_pixel(i, j, M, N):
    print "Rotate pixel: ", i, j
    # Note that the rotation of one element will
    # drive another 3 elements on the same circle to rotate.
    
    # Swap the four elements
    indices = []
    elements = []
    for k in range(0, 4):
        indices.append((i, j))
        elements.append(M[i][j])
        i, j = j, N-i-1
    M[indices[0][0]][indices[0][1]] = elements[3]
    for k in range(1, 4):
        M[indices[k][0]][indices[k][1]] = elements[k-1]

It took me more than one hour to implment rotate_pixel because it's not as easy as I thought before. Pay attention to
i, j = j, N-i-1
because it is different from
i = j
j = N-i-1
The later one is equivalent to
i = j
j = N-j-1
which caused a bug in my first version of implementation.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值