题目链接:48. 旋转图像
思路:模拟。逐行旋转。
代码:
class Solution { public void rotate(int[][] matrix) { for(int i=0; i<((matrix.length + 1)>>1); i++){ helper(matrix, i); } } private void helper(int[][] matrix, int rowIndex){ int len = matrix.length - 1; for(int i=rowIndex; i<len-rowIndex; i++){ int t = matrix[rowIndex][i]; matrix[rowIndex][i] = matrix[len - i][rowIndex]; matrix[len - i][rowIndex] = matrix[len - rowIndex][len - i]; matrix[len - rowIndex][len - i] = matrix[i][len - rowIndex]; matrix[i][len - rowIndex] = t; } } }