leetcode Rotate Image

就是转圈圈!

代码:

#include<iostream>
#include<vector>

using namespace std;

void rotate(vector<vector<int>>& matrix) 
{
    int L = matrix.size();
    int i = 0;
    while (i < L / 2)
    {
        int j = 0;
        while (j < L - i*2-1)
        {
            int c = matrix[i][i+j];
            matrix[i][i+j] = matrix[L-i-j-1][i];
            matrix[L - j- i - 1][i] = matrix[L-i-1][L-j-i-1];
            matrix[L - i - 1][L - j-i - 1] = matrix[i+j][L - i - 1];
            matrix[i+j][L - i - 1] = c;
            j++;
        }
        i++;
    }
}

int main()
{
    vector<vector<int>> matrix = 
    {
        {1, 2, 3, 4 },
        {5, 6, 7, 8 },
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    rotate(matrix);
    for (int i = 0; i < matrix.size(); i++)
    {
        for (int j = 0; j < matrix.size(); j++)
        {
            cout << matrix[i][j] << "    ";
        }
        cout << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chaiwentao/p/4601025.html