zoukankan      html  css  js  c++  java
  • [leetcode]48RotateImage二维数组翻转

    import java.util.Arrays;
    
    /**
     * 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?
    
    翻转二维数组,思路是把数组分成length/2层,每层分别翻转,左边的到上边,下边的到左边,右边的到下边,上边的到右边
     每层也有一个循环,设置一个中间量,这层的数一个一个交换,注意每层循环的次数(就是这层每条边的数量-1)
     */
    public class Q48RotateImage {
        public static void main(String[] args) {
            int[][] matrix = new int[][]{{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
            rotate(matrix);
            for (int[] num :
                    matrix) {
                System.out.println(Arrays.toString(num));
            }
    
        }
        public static void rotate(int[][] matrix) {
            int temp;
            int l = matrix.length;
            for (int i = 0; i < l / 2; i++) {
                for (int j = 0; j < l-1-(2*i); j++) {
                    temp = matrix[i][i+j];
                    matrix[i][i+j] = matrix[l-1-i-j][i];
                    matrix[l-1-i-j][i] = matrix[l-1-i][l-1-i-j];
                    matrix[l-1-i][l-1-i-j] = matrix[i+j][l-1-i];
                    matrix[i+j][l-1-i] = temp;
                }
            }
        }
    }
  • 相关阅读:
    js参数自定义
    分页插件--记录
    .net mvc接收参数为null的解决方案
    c#枚举转字典或表格
    openlayers添加弹出框
    openlayers按坐标点播放
    openlayers轨迹匀速播放
    MyEclipse配置进行Hibernate逆映射
    BIO,NIO,AIO
    Git遇到的一点错误
  • 原文地址:https://www.cnblogs.com/stAr-1/p/7117623.html
Copyright © 2011-2022 走看看