Share my java solution with two step


  • 0
    S
    public static void rotate(int[][] matrix) {
    	int n = matrix.length;
    	int tmp = 0;
    	
    	//reverse by left<->right
    	for (int i = 0; i < n; i ++) {
    		for (int j = 0; j < n / 2; j ++) {
    			tmp = matrix[i][j];
    			matrix[i][j] = matrix[i][n - 1 - j];
    			matrix[i][n - 1 - j] = tmp;
    		}
    	}
    	
    	//reverse by lefttop<->rightbottom
    	for (int i = 0; i < n; i ++) {
    		for (int j = 0; j < n - i; j ++) {
    			tmp = matrix[i][j];
    			matrix[i][j] = matrix[n - 1 - j][n - 1 - i];
    			matrix[n - 1 - j][n - 1 - i] = tmp;
    		}
    	}
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.