★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bzpdfqrk-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You are given an n x n 2D matrix representing an image.git
Rotate the image by 90 degrees (clockwise).github
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.spa
Example 1:code
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:htm
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 × n 的二維矩陣表示一個圖像。blog
將圖像順時針旋轉 90 度。ip
說明:get
你必須在原地旋轉圖像,這意味着你須要直接修改輸入的二維矩陣。請不要使用另外一個矩陣來旋轉圖像。
示例 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 }