标签:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
题目大意是将矩阵做就地顺时针90度旋转。
1 public class Solution { 2 public void rotate(int[][] matrix) { 3 int n = matrix.length; 4 if(n == 0) return; 5 for(int i = 0;i < n; i++){ 6 for(int j = 0;j < n-i; j++){//沿着副对角线旋转一次。 7 int tmp = matrix[i][j]; 8 matrix[i][j] = matrix[n-j-1][n-i-1]; 9 matrix[n-j-1][n-i-1] = tmp; 10 } 11 } 12 13 for(int i = 0;i < n/2; i++){ 14 for(int j = 0;j < n; j++){//沿着中轴线,上下交换一次元素 15 int tmp = matrix[i][j]; 16 matrix[i][j] = matrix[n-i-1][j]; 17 matrix[n-i-1][j] = tmp; 18 } 19 } 20 } 21 }
48. Rotate Image java solutions
标签:
原文地址:http://www.cnblogs.com/guoguolan/p/5621115.html