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

[Swift]LeetCode48. 旋转图像 | Rotate Image,第1张

概述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 dire

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,2],[9,6,3]]

Example 2:

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

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

将图像顺时针旋转 90 度。

说明:

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

示例 1:

给定 matrix = [  [1,原地旋转输入矩阵,使其变为:[  [7,3]]

示例 2:

给定 matrix =[  [ 5,原地旋转输入矩阵,使其变为:[  [15,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].count10             {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                 break12             }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] = firstPoint20             }21             //向内层靠近22             currentLayerWIDth = currentLayerWIDth - 223         }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 - start11                 (matrix[start][i],matrix[i][end],matrix[end][end-offset],matrix[end-offset][start]) = ( matrix[end-offset][start],matrix[start][i],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] = temp17             }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 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode48. 旋转图像 | Rotate Image全部内容,希望文章能够帮你解决[Swift]LeetCode48. 旋转图像 | Rotate Image所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1023064.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存