Leetcode 48 Solution

This article provides solution to leetcode question 48 (rotate-image)

https://leetcode.com/problems/rotate-image

Solution

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        auto& a = matrix;
        int n = matrix.size();

        for (int i = 0; i < n / 2; i++)
        {
            for (int j = 0; j < (n + 1) / 2; j++)
            {
                int tmp = a[i][j];
                a[i][j] = a[n - 1 - j][i];
                a[n - 1 - j][i] = a[n - 1 - i][n - 1 - j];
                a[n - 1 - i][n - 1 - j] = a[j][n - 1 - i];
                a[j][n - 1 - i] = tmp;
            }
        }
    }
};