leetcode_c++:Rotate Image(048)

来源:互联网 发布:大数据时代如何赚钱 编辑:程序博客网 时间:2024/05/18 00:53

题目

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?


算法

复杂度:O(n^2)

  1. 对角线swap
  2. 行反转

class Solution {public:    void rotate(vector<vector<int> >& matrix) {        int n=matrix.size();        for(int i=0;i<n;i++)            for(int j=i+1;j<n;j++)                swap(matrix[i][j],matrix[j][i]);        for(int i=0;i<n;i++)            reverse(matrix[i].begin(),matrix[i].end());    }};
0 0
原创粉丝点击