【LeetCode】Rotate Image

来源:互联网 发布:诺基亚6600s软件 编辑:程序博客网 时间:2024/05/18 11:47

题目描述:

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) {//先左右翻转int N = matrix.size();for (int i = 0; i < N;i++)for (int j = 0; j < N / 2; j++)swap(matrix[i][j], matrix[i][N - 1 - j]);//再以右斜线为轴翻转for (int i = 0; i < N;i++)for (int j = 0; j < N - 1 - i; j++)swap(matrix[i][j], matrix[N - 1 - j][N - 1 - i]);}};


0 0
原创粉丝点击