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 26 27 28 29 30 31 32 33 34
| package com.demo.s48;
public class Solution { public void rotate(int[][] matrix) { if(matrix.length == 0 || matrix.length != matrix[0].length) { return; } int nums = matrix.length; for (int i = 0; i < nums; ++i){ for (int j = 0; j < nums - i; ++j){ int temp = matrix[i][j]; matrix[i][j] = matrix[nums - 1 - j][nums - 1 - i]; matrix[nums - 1 - j][nums - 1 - i] = temp; } } for (int i = 0; i < (nums >> 1); ++i){ for (int j = 0; j < nums; ++j){ int temp = matrix[i][j]; matrix[i][j] = matrix[nums - 1 - i][j]; matrix[nums - 1 - i][j] = temp; } } } }
|