Swift LeetCode 系列之48:rotate-image

https://leetcode.com/problems/rotate-image/description/
沿着副对角反转一次. 在沿着水平线翻转一次即可

class Solution {
    func rotate(_ matrix: inout [[Int]]) {
        let n = matrix.count
        for i in 0 ..< n {
            for j in 0 ..< (n - i) {
                let temp = matrix[i][j]
                matrix[i][j] = matrix[n - 1 - j][n - 1 - i]
                matrix[n - 1 - j][n - 1 - i] = temp
            }
        }
        
         for i in 0 ..< n / 2 {
            for j in 0 ..< n {
                let temp = matrix[i][j]
                matrix[i][j] = matrix[n - 1 - i][j]
                matrix[n - 1 - i][j] = temp
            }
        }
        
    }
}
    原文作者:TimberTang
    原文地址: https://www.jianshu.com/p/3d1999fd4e4c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞