Leetcode 48. Rotate Image

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Rotate Image

2. Solution

  • Version 1
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
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
for(int k = 0; k < n; k++) {
int i = 0;
int j = n - 1;
while(i < j) {
swap(matrix[k][i], matrix[k][j]);
i++;
j--;
}
}
}

private:
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
};

Reference

  1. https://leetcode.com/problems/rotate-image/description/
如果有收获,可以请我喝杯咖啡!