-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48.rotate-image.cpp
More file actions
40 lines (32 loc) · 819 Bytes
/
48.rotate-image.cpp
File metadata and controls
40 lines (32 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* @lc app=leetcode id=48 lang=cpp
*
* [48] Rotate Image
*/
// @lc code=start
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
vector<int> arr;
arr.reserve(matrix.size() * matrix.size());
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix.size(); j++) {
arr.push_back(matrix[i][j]);
}
}
for (int i = 0; i < arr.size(); i++) {
int rowIndex = i / matrix.size();
int colIndex = i % matrix.size();
matrix[colIndex] [matrix.size() - rowIndex - 1] = arr[i];
}
}
};
// @lc code=end