rotate-image

来源:互联网 发布:硬盘修复软件 编辑:程序博客网 时间:2024/06/06 07:11

题目:

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) {        if (matrix.size() == 0)            return ;        if (matrix[0].size() == 0)            return;        int m = matrix.size();        int n = matrix[0].size();        if (m != n)            return;        for (int i = 0; i < m; i++)        {            for (int j = i+1; j < n; j++)            {                swap(matrix[i][j], matrix[j][i]);            }        }        for (int i = 0; i < m; i++)        {            for (int j = 0; j < n/2; j++)            {                swap(matrix[i][j], matrix[i][n-1-j]);            }        }    }};
原创粉丝点击