Rotate Image问题及解法

来源:互联网 发布:centos快速打开终端 编辑:程序博客网 时间:2024/05/29 15:30

问题描述:

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

问题分析:

矩阵顺时针旋转90度可以分为两步:

1.对矩阵转置

2.对矩阵按列reverse(翻转)


过程详见代码:

class Solution {public:    void rotate(vector<vector<int>>& matrix) {        int n = matrix.size();        for(int i = 0; i < n; i++)        {        for(int j = 0; j < i; j++)        {        swap(matrix[i][j],matrix[j][i]);}}        for(int i = 0; i < n / 2; i++)        {        for(int j = 0; j < n; j++)        {        swap(matrix[j][i],matrix[j][n - i - 1]);}}    }};


原创粉丝点击