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


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

    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 }
  • 相关阅读:
    java之数组使用以及拓展
    Java之数组介绍
    方法
    break&continue
    networkmanager is not running 网络管理没有运行
    CentOS-7修改IP
    操作系统
    计算机基础理论
    编码和解码
    pyQuery库
  • 原文地址:https://www.cnblogs.com/strengthen/p/9907843.html
Copyright © 2020-2023  润新知