33_leetcode_Rotate Image

来源:互联网 发布:mac跳转指定目录 编辑:程序博客网 时间:2024/06/06 08:23

  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?


    //1:首先考虑特殊情况;2:沿着对角线将矩阵进行对折;2:沿着最中间的列将矩阵进行再次对折。

    void rotate(vector<vector<int> > &matrix) {     if(matrix.size() <= 1 || matrix[0].size() == 0)         return;                int rows = (int)matrix.size();        int columns = (int)matrix[0].size();                //对折        for(int i = 0; i < rows; i++)        {            for(int j = i + 1; j < columns; j++)            {                swap(matrix[i][j], matrix[j][i]);                            }        }        //对折        for(int i = 0; i < rows; i++)        {            int start = 0;            int end = columns-1;                        while(start < end)            {                swap(matrix[i][start++], matrix[i][end--]);            }        }                return;    }


0 0
原创粉丝点击