美文网首页Swift LeetCode
Swift LeetCode 系列之48:rotate-imag

Swift LeetCode 系列之48:rotate-imag

作者: TimberTang | 来源:发表于2017-11-29 10:19 被阅读44次

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
            }
        }
        
    }
}

相关文章

网友评论

    本文标题:Swift LeetCode 系列之48:rotate-imag

    本文链接:https://www.haomeiwen.com/subject/fdcubxtx.html