LeetCode Rotate Image

来源:互联网 发布:js确认框 编辑:程序博客网 时间:2024/05/29 18:46

ou 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?

因为顺时针选择90度是图形旋转中的特殊情况,通过找到特殊情况中的元素之间的规律进行求解。

思路一:将矩阵转置,然后将矩阵每一行逆序,即为所求解

代码如下:

class Solution {public:    void rotate(vector<vector<int>>& matrix) {        int len = matrix.size();        int count=0;        for(int i=0;i<len;i++)        {            for(int j=i+1;j<len;j++)            {                matrix[i][j] ^= matrix[j][i];                matrix[j][i] ^= matrix[i][j];                matrix[i][j] ^= matrix[j][i];            }        }                for(int i=0;i<len;i++)           reverse(matrix[i].begin(),matrix[i].end());    }};






0 0
原创粉丝点击