码迷,mamicode.com
首页 > 其他好文 > 详细

Rotate Image

时间:2016-07-19 09:44:04      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).

Example

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 }

 

 

Rotate Image

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5683342.html

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