Cover image for Classic 150 Interview Questions P48 Rotate Image

Classic 150 Interview Questions P48 Rotate Image


Timeline

Timeline

2025-11-17

init

Matrix

Problem:

First flip along the diagonal, then reverse each row.

12345678910111213141516171819
#include <vector>#include <algorithm>using std::vector;class Solution {    public:	void rotate(vector<vector<int> > &matrix)	{		// First flip along the diagonal		int i, j, n = matrix.size();		for (i = 0; i < n; i++)			for(j = i + 1; j< n; j++)				std::swap(matrix[i][j], matrix[j][i]);		// Then reverse each row		for (i = 0; i < n; i++)			std::reverse(matrix[i].begin(), matrix[i].end());	}};

leetcode hot 100 rewrite

123456789101112131415161718
#include <vector>#include <algorithm>using std::vector;class Solution {    public:        void rotate(vector<vector<int> > &matrix)        {                int i, j, n = matrix.size();                for (i = 0; i < n; i++) {                        for (j = i + 1; j < n; j++) {                                std::swap(matrix[i][j], matrix[j][i]);                        }                }                for (i = 0; i < n; i++)                        std::reverse(matrix[i].begin(), matrix[i].end());        }};
Loading comments…