The problem is to give you a 2D image (we know that the 2D image is made up of (x, y) pixels), the topic requires us to rotate the graph clockwise 90 degrees, is indeed a more interesting topic.
If this is the first time you've got the problem, it's really hard to do it, but once you've seen a similar topic (for example, the spiral array on the offer), then you have a moment of thought. Let me draw a diagram to explain the idea of the problem:
The main idea is also from the outside, each rotation off a layer, repeat the above operation, know the loop to the innermost, this time completed the overall rotation. Code:
Class Solution {Public:void rotate (vector<vector<int>>& matrix) {int num = matrix.size (); for (int i = 0; i < NUM/2; ++i) {//rotate one layer at a time rotateone (matrix, I);}} void Rotateone (vector<vector<int>>& matrix, int begin) {int len = matrix.size ()-2 * begin-1;int tmp = 0 ; for (int i = len; I >= 1; i) {//rotate TMP = matrix[begin][begin + I];matrix[begin][begin + i] = Matrix[begin + len-i][ Begin];matrix[begin + Len-i][begin] = matrix[begin + len][begin + len-i];matrix[begin + len][begin + len-i] = Matrix [Begin + I] [begin + Len];matrix[begin + i][begin + len] = tmp;}};
Results:
Written question 61. Leetcode OJ (48)