zoukankan      html  css  js  c++  java
  • 48. Rotate Image java solutions

    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?

    题目大意是将矩阵做就地顺时针90度旋转。   

     1 public class Solution {
     2     public void rotate(int[][] matrix) {
     3         int n = matrix.length;
     4         if(n == 0) return;
     5         for(int i = 0;i < n; i++){
     6             for(int j = 0;j < n-i; j++){//沿着副对角线旋转一次。
     7                 int tmp = matrix[i][j];
     8                 matrix[i][j] = matrix[n-j-1][n-i-1];
     9                 matrix[n-j-1][n-i-1] = tmp;
    10             }
    11         }
    12         
    13         for(int i = 0;i < n/2; i++){
    14             for(int j = 0;j < n; j++){//沿着中轴线,上下交换一次元素
    15                 int tmp = matrix[i][j];
    16                 matrix[i][j] = matrix[n-i-1][j];
    17                 matrix[n-i-1][j] = tmp;
    18             }
    19         }
    20     }
    21 }
  • 相关阅读:
    生涯路
    事件与window的基本操作
    js操作
    c# 函数
    布局页面CSS
    网页填写的基本操作
    框架集
    网页的基本操作1
    存储与触发器
    常用的函数
  • 原文地址:https://www.cnblogs.com/guoguolan/p/5621115.html
Copyright © 2011-2022 走看看