首页 > 其他 > 详细

[Leetcode]-- Rotate Image

时间:2014-02-08 09:09:32      阅读:580      评论: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?

 

参考:

水中的鱼: [LeetCode] Rotate Image 解题报告

[解题思路]
如下图,首先沿逆对角线翻转一次,然后按x轴中线翻转一次。

bubuko.com,布布扣
 
bubuko.com,布布扣
 1 public class Solution {
 2    public void rotate(int[][] matrix) {
 3         int len = matrix.length;
 4         if(len == 0)
 5             return;
 6         
 7         for(int i = 0; i < len - 1; i++){
 8             for(int j = 0; j < len -i; j++){
 9                 swap(matrix, i, j, len - 1 - j, len - 1 -i);
10             }
11         }
12         
13         
14         for(int i = 0; i < len / 2; i++){
15             for(int j = 0; j < len; j++){
16                 swap(matrix, i, j, len - 1 -i, j);
17             }
18         }
19     }
20     
21     public void swap(int[][] matrix, int i, int j, int m, int n){
22         int tmp = matrix[i][j];
23         matrix[i][j] = matrix[m][n];
24         matrix[m][n] = tmp;
25     }
26 }
View Code

 

[Leetcode]-- Rotate Image

原文:http://www.cnblogs.com/RazerLu/p/3540015.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!