Leetcode 048 Rotate Image(模拟)

xiaoxiao2021-02-28  65

题目连接:Leetcode 048 Rotate Image

解题思路:将矩阵分为4块,每次交换4个数。

                1    1    1    1    2

                4    1    1    1    2

                4    4    *    2   2

                4    3    3    3   2

                4    3    3    3   3

class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for (int i = 0; i < n; i++) { for (int j = i; j < n-i-1; j++) { int tmp = matrix[i][j]; matrix[i][j] = matrix[n-j-1][i]; matrix[n-j-1][i] = matrix[n-i-1][n-j-1]; matrix[n-i-1][n-j-1] = matrix[j][n-i-1]; matrix[j][n-i-1] = tmp; } } } };
转载请注明原文地址: https://www.6miu.com/read-2619398.html

最新回复(0)