Today in Lintcode did a problem, I think about the practice, made, but felt it necessary to record.
Test instructions
Given a nxn two-dimensional matrix to represent the image, 90 degrees clockwise rotation of the image.
Examples:
Given a rectangle [[1,2],[3,4]],90] Clockwise, return [[3,1],[4,2]]
Challenge:
Can you do it in situ?
1. Solving ideas
At first glance, this problem is really very simple. Here I use a graph to express the problem-solving ideas:
2. Code
1 Public voidRotateint[] matrix) {2 if(Matrix.length = = 0 | | matrix[0].length = = 0) {3 return;4 }5 //rotate clockwise 270 degrees6 for(inti = 0; i < matrix.length; i++) {7 for(intj = 0; J < I; J + +) {8 inttemp =Matrix[i][j];9MATRIX[I][J] =Matrix[j][i];TenMatrix[j][i] =temp; One } A } - //rotate counterclockwise 180 degrees - for(inti = 0; i < matrix.length; i++) { the for(intj = 0; J < Matrix[0].length/2; J + +) { - inttemp =Matrix[i][j]; -MATRIX[I][J] = matrix[i][matrix[0].length-1-j]; -Matrix[i][matrix[0].length-1-J] =temp; + } - } +}
Algorithm-rotate image (matrix processing)