lintcode-medium-Rotate Image

Posted 哥布林工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-medium-Rotate Image相关的知识,希望对你有一定的参考价值。

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]
]
Challenge

Do it in-place.

 

public class Solution {
    /**
     * @param matrix: A list of lists of integers
     * @return: Void
     */
    public void rotate(int[][] matrix) {
        // write your code here
        
        if(matrix == null || matrix.length <= 1)
            return;
        
        int n = matrix.length;
        
        for(int i = 0; i < n / 2; i++)
            rotate(matrix, i);
        
        return;
    }
    
    public void rotate(int[][] matrix, int num){
        
        int n = matrix.length;
        
        int start = num;
        int end = n - 1 - num;
        
        for(int i = 0; i < end - start; i++){
            int temp = matrix[start][start + i];
            matrix[start][start + i] = matrix[end - i][start];
            matrix[end - i][start] = matrix[end][end - i];
            matrix[end][end - i] = matrix[start + i][end];
            matrix[start + i][end] = temp;
        }
        
        return;
    }
    
}

 

以上是关于lintcode-medium-Rotate Image的主要内容,如果未能解决你的问题,请参考以下文章

Autel MaxiIM IM608 VS MaxiIM IM508 VS Auro OtoSys IM600 VS IM100

qq的IM是啥意思

IM序列8:基于集群的移动端IM接入层负载均衡方案分享

im即时通讯开发:IM系统中离线消息历史消息实践

Julia - 复数

im即时通讯开发:IM通信协议设计详解