标签:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90
degrees (clockwise).
Given a matrix
[
[1,2],
[3,4]
]
rotate it by 90 degrees (clockwise), return
[
[3,1],
[4,2]
]
分析:
本题的关键是找到几个互换点之间位置的关系。
1 public class Solution { 2 /** 3 * @param matrix: 4 * A list of lists of integers 5 * @return: Void 6 */ 7 public void rotate(int[][] matrix) { 8 if (matrix == null || matrix.length == 0) return; 9 int n = matrix.length - 1; 10 for (int i = 0; i < (n + 1) / 2; ++i) { 11 for (int j = i; j < n - i; ++j) { 12 int t = matrix[i][j]; 13 matrix[i][j] = matrix[n - j][i]; 14 matrix[n - j][i] = matrix[n - i][n - j]; 15 matrix[n - i][n - j] = matrix[j][n - i]; 16 matrix[j][n - i] = t; 17 } 18 } 19 } 20 }
标签:
原文地址:http://www.cnblogs.com/beiyeqingteng/p/5683342.html