[leetcode] Rotate Image

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?

我采用的是比较笨拙的方法,拷贝到另一个空间中去

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int n=matrix.size();
        vector<vector<int> > ret(matrix); 
        for(int j=0 ; j<n ; j++){
            for(int i=0 ; i<n ; i++){
                ret[j][n-i-1]=matrix[i][j];
            }
        }
        for(int i=0 ; i<n ; i++)
            copy(ret[i].begin(),ret[i].end(),matrix[i].begin());
    }
};


你可能感兴趣的:([leetcode] Rotate Image)