LeetCode 48 旋转图像

原文地址:https://leileiluoluo.com/posts/leetcode-rotate-image.html

1 题目描述
给定一个 n x n 的二维矩阵表征一幅图像,求顺时针旋转90度后的图像。
注:你必须使用现有内存空间原地旋转该图像,勿分配额外的二维矩阵空间来做旋转。

例子1:
输入矩阵:
[
[1,2,3],
[4,5,6],
[7,8,9]
]
输出:
[
[7,4,1],
[8,5,2],
[9,6,3]
]

例子2:
输入矩阵:
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
输出:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

题目出处:
https://leetcode.com/problems/rotate-image/

2 解决思路
1)如图所示,原始图如下图所示,要不分配额外空间,以最小的空间来作旋转,我们设定每次仅移动一位。


2)一圈元素顺时针旋转90度的移动过程如下图左图所示,仅申请一个元素的存储单元tmp,作顺时针旋转时,我们每次仅移动一位。步骤如下。
2.1)左上角元素移入tmp;
2.1)最左边一列元素均向上移动一位;
2.2)最底部一行元素均向左移动一位;
2.3)最右边一列元素均向下移动一位;
2.4)最顶部一行元素均向右移动一位;
2.5)tmp元素置入顶部行的最左边第二列;
2.6)循环移动n-1次,直至下图右图所示,整个最外围一圈的元素即顺时针90度旋转完成。
3)最外围边界递进一层,开始次外围一圈元素的旋转,移动方式同2-3相同。
4)直至最里边一圈的元素均旋转完成,即得到整个图像的旋转。

3 golang实现代码
https://github.com/olzhy/leetcode/blob/master/48_Rotate_Image/test.go

func rotate(matrix [][]int) {  
    for level := 0; level < len(matrix)/2; level++ {  
        topBoundary := level  
        leftBoundary := level  
        bottomBoundary := len(matrix) - 1 - level  
        rightBoundary := len(matrix) - 1 - level  
        for times := 0; times < bottomBoundary-topBoundary; times++ {  
            // left  
            mostLeftTop := matrix[topBoundary][leftBoundary]  
            for row := topBoundary; row < bottomBoundary; row++ {  
                matrix[row][leftBoundary] = matrix[row+1][leftBoundary]  
            }  
  
            // bottom  
            for col := leftBoundary; col < rightBoundary; col++ {  
                matrix[bottomBoundary][col] = matrix[bottomBoundary][col+1]  
            }  
  
            // right  
            for row := bottomBoundary; row > topBoundary; row-- {  
                matrix[row][rightBoundary] = matrix[row-1][rightBoundary]  
            }  
  
            // top  
            for col := rightBoundary; col > leftBoundary+1; col-- {  
                matrix[topBoundary][col] = matrix[topBoundary][col-1]  
            }  
            matrix[topBoundary][leftBoundary+1] = mostLeftTop  
        }  
    }  
}  

4 基准测试

goos: darwin  
goarch: amd64  
pkg: github.com/olzhy/test  
BenchmarkRotate-4     20000000                65.2 ns/op             0 B/op          0 allocs/op  
PASS  
ok      github.com/olzhy/test   1.383s  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值