leetcode刷题,总结,记录 ,备忘48

leetcode48

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) {
             vector<vector<int>> temp;
             int i = 0, j = 0;
             vector<int> t;
             
             for (; j < matrix.size(); ++j)
             {
                 for (i = 0 ; i < matrix.size(); ++i)
                 {
                     t.push_back(matrix[i][j]);
                 }
                 temp.push_back(vector<int>(t.rbegin(), t.rend()));
                 t.clear();
             }
             
             matrix = temp;
        }
    };


你可能感兴趣的:(leetcode刷题,总结,记录 ,备忘48)