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