48.图像旋转

xiaoxiao2021-02-28  86

Rotate Image

问题描述:

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 size = matrix.size(); int temp = -1; vector<vector<int>> result(size,vector<int>(size,-1)); for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { result[j][size-i-1] = matrix[i][j]; } } matrix = result; } };

性能:

参考答案:

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

性能:

转载请注明原文地址: https://www.6miu.com/read-19376.html

最新回复(0)