首页 > 编程语言 > 详细

48. Rotate Image java solutions

时间:2016-06-27 19:44:42      阅读:173      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!