My Java Solution but looks a little awkward.


  • 0
    G
    public class Solution {
        public void rotate(int[][] matrix) {
        	int tmp,
        	n = matrix.length;
        	for (int i = 0; i <= (n-1)/2; i++) {
    			for (int j = i; j < n-i-1; j++) {
    				int times = 4;
    				int tmpI = i;
    				int tmpJ = j;
    				tmp = matrix[i][j];
    				while (times-- > 0) {
    					int a = tmpJ;
    					int b = n - tmpI - 1;
    					int c = tmp;
    					tmp = matrix[a][b];
    					matrix[a][b] = c;
    					tmpI = a;
    					tmpJ = b;
    				}
    			}
    		}
        }
    }

Log in to reply
 

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