leetcode hot100 旋转图像

48. 旋转图像

已解答

中等

相关标签

相关企业

给定一个 × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像

class Solution(object):

    def rotate(self, matrix):

        """

        :type matrix: List[List[int]]

        :rtype: None Do not return anything, modify matrix in-place instead.

        """

        # 旋转四次之后就是原图像

        # 原地修改不能使用visted

       

        n=len(matrix[0])

        count=(n+1)/2

        if n%2==0:

            for counti in range(count):

                # s=matrix[i][j]

                top = counti

                bottom = n-counti-1

                left = counti

                right = n-counti-1

                for swap_count in range(0,n-1-2*counti):

                    tmp = matrix[top+swap_count][right]

                    matrix[top+swap_count][right] = matrix[top][left+swap_count]

                    tmp2 = matrix[bottom][right-swap_count]

                    matrix[bottom][right-swap_count] = tmp

                    tmp3 = matrix[bottom-swap_count][left]

                    matrix[bottom-swap_count][left] = tmp2

                    matrix[top][left+swap_count]=tmp3

        else:

            for counti in range(count):

                # s=matrix[i][j]

                top = counti

                bottom = n-counti-1

                left = counti

                right = n-counti-1

                for swap_count in range(0,n-1-2*counti):

                    tmp = matrix[top+swap_count][right]

                    matrix[top+swap_count][right] = matrix[top][left+swap_count]

                    tmp2 = matrix[bottom][right-swap_count]

                    matrix[bottom][right-swap_count] = tmp

                    tmp3 = matrix[bottom-swap_count][left]

                    matrix[bottom-swap_count][left] = tmp2

                    matrix[top][left+swap_count]=tmp3

                if counti==count-1:

                    break




 

       



 

         

这里其实可以看作是左上角的四分之一的矩阵进行旋转得到的全部,而我使用的是一圈一圈的方法。注意奇偶不一样导致的最终可能里面一圈是一个数或者一圈数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值