classSolution { publicvoidrotate(int[][] matrix) { intleft=0, right = matrix[0].length - 1, up = 0, down = matrix.length - 1; while (left < right) { for (inti= left; i < right; i++) { // element in row up. inttemp= matrix[up][i]; // row up to col right temp = swap(matrix, temp, i - left + up, right); // col right to row down temp = swap(matrix, temp, down, right - (i - left)); // row down to col left temp = swap(matrix, temp, down - (i - left), left); // col left to row up temp = swap(matrix, temp, up, i); } left += 1; right -= 1; up += 1; down -= 1; } }
privateintswap(int[][] matrix, int tmp, int row, int col) { inttemp= matrix[row][col]; matrix[row][col] = tmp; return temp; } }