4.34 leetcode -34 rotate-image

来源:互联网 发布:项目管理 源码 编辑:程序博客网 时间:2024/05/17 21:59

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) {        int n = matrix.size();        vector<vector <int> > matrixtmp(n ,vector<int>(n));        for(int i = 0;i < n;i++)            {            for(int j = 0;j < n;j ++)                {                matrixtmp[j][n-i-1] = matrix[i][j];            }        }        for(int i = 0;i < n;i++)            {            for(int j = 0;j < n;j ++)                {                matrix[i][j] = matrixtmp[i][j];            }        }    }};


原创粉丝点击