leetcode 48. Rotate Image

来源:互联网 发布:数据清洗的方法不包括 编辑:程序博客网 时间:2024/05/21 07:11
//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?public class Solution {public static void rotate(int[][] matrix) {        int row;//标记当前圈数下的数组表示下的最小范围        int col;//正数增加        int length = matrix.length;        int edge = length-1;//标记当前圈数下的数组表示下的最大范围        int nEdge;//从最大值的倒数        for(row = 0;row<edge;row++){//向内一圈            nEdge = edge;            for(col = row;col<edge;col++,nEdge--){//col = row 保证了按照从外到内一圈一圈的进行遍历;在本圈内进行交换旋转                int val=matrix[row][col];                matrix[row][col] = matrix[nEdge][row];                matrix[nEdge][row] = matrix[edge][nEdge];                matrix[edge][nEdge] = matrix[col][edge];                matrix[col][edge] = val;            }            edge--;//向内一圈后,最大值也要减1        }    }public static void main(String[] args) {int[][] input = {{1,2,3},{4,5,6},{7,8,9}};rotate(input);for(int i = 0;i<input.length;i++){for(int j= 0;j<input.length;j++){System.out.print(input[i][j]);}System.out.println();}}    }

0 0
原创粉丝点击