题目:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
题解:
如此简单的一道题竟然搞了好多错误。一个是offset要注意,一个是内循环次数要注意。
class Solution { public: void rotate(vector<vector<int> > &matrix) { for(int i = 0; i < matrix.size() / 2; i++) { int start = i; int end = matrix.size()-1-i; for(int j = start; j < end; j++) { int offset = j - start; int temp = matrix[i][j]; matrix[i][j] = matrix[end-offset][i]; matrix[end-offset][i] = matrix[end][end-offset]; matrix[end][end-offset] = matrix[j][end]; matrix[j][end] = temp; } } } };