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] ]
在計算機圖像處理里,旋轉圖片是很常見的,由於圖片的本質是二維數組,所以也就變成了對數組的操作處理,翻轉的本質就是某個位置上數移動到另一個位置上,比如用一個簡單的例子來分析:
1 2 3 7 4 1 4 5 6 --> 8 5 2 7 8 9 9 6 3
對於90度的翻轉有很多方法,一步或多步都可以解,先來看一種直接的方法,這種方法是按順時針的順序去覆蓋前面的數字,從四個頂角開始,然后往中間去遍歷,每次覆蓋的坐標都是同理,如下:
(i, j) <- (n-1-j, i) <- (n-1-i, n-1-j) <- (j, n-1-i)
這其實是個循環的過程,第一個位置又覆蓋了第四個位置,這里i的取值范圍是 [0, n/2),j的取值范圍是 [i, n-1-i),至於為什么i和j是這個取值范圍,為啥i不用遍歷 [n/2, n),若仔細觀察這些位置之間的聯系,不難發現,實際上j列的范圍 [i, n-1-i) 順時針翻轉 90 度,正好就是i行的 [n/2, n) 的位置,這個方法每次循環換四個數字,如下所示:
1 2 3 7 2 1 7 4 1 4 5 6 --> 4 5 6 --> 8 5 2 7 8 9 9 8 3 9 6 3
解法一:
class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for (int i = 0; i < n / 2; ++i) { for (int j = i; j < n - 1 - i; ++j) { int tmp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - j][i]; matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]; matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]; matrix[j][n - 1 - i] = tmp; } } } };
還有一種解法,首先以從對角線為軸翻轉,然后再以x軸中線上下翻轉即可得到結果,如下圖所示(其中藍色數字表示翻轉軸):
1 2 3 9 6 3 7 4 1 4 5 6 --> 8 5 2 --> 8 5 2 7 8 9 7 4 1 9 6 3
解法二:
class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - i; ++j) { swap(matrix[i][j], matrix[n - 1- j][n - 1 - i]); } } reverse(matrix.begin(), matrix.end()); } };
最后再來看一種方法,這種方法首先對原數組取其轉置矩陣,然后把每行的數字翻轉可得到結果,如下所示(其中藍色數字表示翻轉軸,Github 上可能無法顯示顏色,請參見博客園上的帖子):
1 2 3 1 4 7 7 4 1 4 5 6 --> 2 5 8 --> 8 5 2 7 8 9 3 6 9 9 6 3
解法三:
class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { swap(matrix[i][j], matrix[j][i]); } reverse(matrix[i].begin(), matrix[i].end()); } } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/48
參考資料:
https://leetcode.com/problems/rotate-image/
https://leetcode.com/problems/rotate-image/discuss/18895/Clear-Java-solution
https://leetcode.com/problems/rotate-image/discuss/18872/A-common-method-to-rotate-the-image