-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path自己写的_使用了额外空间.cpp
56 lines (48 loc) · 1.1 KB
/
自己写的_使用了额外空间.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。
不占用额外内存空间能否做到?
示例 1:
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例 2:
给定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
原地旋转输入矩阵,使其变为:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-matrix-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。*/
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int length=matrix.size();
int width=matrix[0].size();
vector<vector<int>> tmp(length,vector<int>(width,0));
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
tmp[i][j]=matrix[length-j-1][i];
}
}
matrix=tmp;
}
};