Seawolf 发表于 2019-9-29 02:56:14

leetcode 48. Rotate Image


You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
,
,

],

rotate the input matrix in-place such that it becomes:
[
,
,

]
Example 2:

Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
,

],

rotate the input matrix in-place such that it becomes:
[
,
,
,

]

class Solution {
    public void rotate(int[][] matrix) {
      if(matrix.length == 0) return;
      int row = matrix.length -1;
      int col = matrix.length -1;
      int repeat = 0;
      for(int i = 0 ; i <= row/2; i++){
            
            for(int j = 0 ; j <= col /2 ; j++){
               
                if(row % 2 == 0 && i == row/2 && j != col/2) continue;
                swap(matrix, i ,j);
               
            }
      }
    }
   
    public void swap(int[][] matrix, int i, int j){
      int row = matrix.length - 1;
      int col = matrix.length - 1;
      
      int temp = matrix;
      matrix = matrix;
      matrix = matrix;
      matrix = matrix;
      matrix = temp;
      
    }
   
}
页: [1]
查看完整版本: leetcode 48. Rotate Image