• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode48. 旋转图像 | Rotate Image

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9907843.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],
rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

Example 2:

Given input matrix =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 
rotate the input matrix in-place such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]

给定一个 × n 的二维矩阵表示一个图像。

将图像顺时针旋转 90 度。

说明:

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

示例 1:

给定 matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],
原地旋转输入矩阵,使其变为:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

示例 2:

给定 matrix =
[
  [ 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]
]

8ms
 1 class Solution 
 2 {
 3     func rotate(_ matrix: inout [[Int]]) 
 4     {
 5         let temp = matrix;
 6         
 7         for i in 0..<matrix.count
 8         {
 9             for j in 0..<matrix[0].count
10             {
11                 matrix[j][matrix[0].count - 1 - i] = temp[i][j];
12             }
13         }
14     }
15 }

12ms

 1 class Solution {
 2     func rotate(_ matrix: inout [[Int]]) {    
 3         matrix.reverse()
 4         
 5         for i in 1..<matrix.count {
 6             for j in 0..<i {
 7                 (matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
 8             }
 9         }
10     }
11 }

16ms

 1 class Solution {
 2     func rotate(_ matrix: inout [[Int]]) {
 3         let layers:Int = matrix.count / 2 - 1
 4         let n = matrix.count - 1
 5         var currentLayerWidth = n - 1    //layer的宽度,每一层转 currentLayerWidth次
 6         if layers < 0 {
 7             return 
 8         }
 9         for i in 0...layers {
10             if currentLayerWidth < 0 {
11                 break
12             }
13             for j in 0...currentLayerWidth {  //每层旋转逻辑
14                 let x = i + j          //i 层级,x 移动点(相对整个矩阵)
15                 let firstPoint = matrix[i][x]
16                 matrix[i][x] = matrix[n - x][i]
17                 matrix[n - x][i] = matrix[n - i][n - x]
18                 matrix[n - i][n - x] = matrix[x][n - i]
19                 matrix[x][n - i] = firstPoint
20             }
21             //向内层靠近
22             currentLayerWidth = currentLayerWidth - 2
23         }
24     }
25 }

16ms

 1 class Solution {
 2     func rotate(_ matrix: inout [[Int]]) {
 3         let n = matrix.count
 4         
 5         for layer in 0..<n/2 {
 6             let start = layer
 7             let end = n - layer - 1
 8             
 9             for i in start..<end {
10                 let offset = i - start
11                 (matrix[start][i], matrix[i][end], matrix[end][end-offset], matrix[end-offset][start]) = ( matrix[end-offset][start], matrix[start][i], matrix[i][end], matrix[end][end-offset])
12             }
13         }
14     }
15 }

20ms

 1 class Solution {
 2     func rotate(_ matrix: inout [[Int]]) {
 3         let count = matrix[0].count
 4         for i in (0 ..< count)  {
 5             for t in (i ..< count) {
 6                 let d = matrix[t][i]
 7                 matrix[t][i] = matrix[i][t]
 8                 matrix[i][t] = d 
 9             }
10         }
11 
12         for i in (0 ..< count) {
13             for t in (0 ..< (count + 1)/2 ) {
14                 let temp = matrix[i][t]
15                 matrix[i][t] = matrix[i][count - t - 1]
16                 matrix[i][count - t - 1] = temp
17             }
18         }
19     }
20 }

28ms

 1 class Solution {
 2     func rotate(_ matrix: inout [[Int]]) {
 3         let count = matrix.count
 4         for i in 0..<count {
 5             for j in i+1..<count {
 6                 if i == j { return }
 7                 // swap
 8                 matrix[i][j] = matrix[i][j] ^ matrix[j][i]
 9                 matrix[j][i] = matrix[i][j] ^ matrix[j][i]
10                 matrix[i][j] = matrix[i][j] ^ matrix[j][i]
11             }
12             matrix[i].reverse()
13         }
14     }
15 }

36ms

 1 class Solution {
 2     func rotate(_ matrix: inout [[Int]]) {
 3 
 4         for item in 0...matrix.count-1 {
 5             for i in item...matrix.count-1 {
 6                 let tmpItem = matrix[item][i]
 7                 matrix[item][i] = matrix[i][item]
 8                 matrix[i][item] = tmpItem
 9             }
10         }
11         for item in 0...matrix.count-1 {
12             matrix[item].reverse()
13         }
14         
15     }
16 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap