码迷,mamicode.com
首页 > 编程语言 > 详细

48. Rotate Image java solutions

时间:2016-06-27 19:47:14      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!