Rotate Image (link)Mar 18 '12
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?
Could you do this in-place?
class Solution { public: void rotate(vector<vector<int> > &matrix) { // Start typing your C/C++ solution below // DO NOT write int main() function if (matrix.empty() || matrix[0].empty() ) return; int const max = matrix.size() - 1; int const quarter_col_size = matrix.size() / 2 + (matrix.size()%2); int const quarter_row_size = matrix.size() / 2; for(int row = 0; row < quarter_row_size; ++row){ for(int col = 0; col < quarter_col_size; ++col){ int const temp = matrix[row][col]; matrix[row][col] = matrix[max-col][row]; matrix[max-col][row] = matrix[max-row][max-col]; matrix[max-row][max-col] = matrix[col][max-row]; matrix[col][max-row] = temp; } } } };
No comments:
Post a Comment