forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rotate_Image.cpp
46 lines (40 loc) · 960 Bytes
/
Rotate_Image.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
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 15, 2012
Problem: Rotate Image
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
Solution:
First swap left half and right half, then swap left top half with right bottom
half.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
swap(matrix[i][j], matrix[i][n - j - 1]);
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
swap(matrix[i][j], matrix[n - j - 1][n - i - 1]);
}
}
}
};