- Swift LeetCode 系列之48:rotate-imag
- Swift LeetCode 系列之 7: Reverse In
- Swift vs. Kotlin 漫谈系列之接口
- Best Time to Buy and Sell Stock
- Swift LeetCode 系列之46: permutatio
- Swift LeetCode 系列之 1: TwoSum
- Swift LeetCode 系列之 19: mergeTwoL
- Swift LeetCode 系列之 13: roman-to-
- Swift LeetCode 系列之 19: Remove Nt
- Swift LeetCode 系列之9: palindrome-
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
}
}
}
}






网友评论