---
# System prepended metadata

title: 0048. Rotate Image
tags: [Leetcode, Medium]

---

#    0048. Rotate Image
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/rotate-image/
## 思路
四个为一组换一圈
![](https://i.imgur.com/ZOVlBIB.jpg)
![](https://i.imgur.com/c0BKG3J.jpg)
注意找起始点的方法是**取最左上的起始点**，例如上面$3*3$的矩阵就是取1和4，$4*4$的矩阵就是取5,1,2,4
## 注意
对于二维向量，C++里面 matrix.size可以表示行数 matrix[0].size表示列数
## Code
```c=
class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int edge = matrix.size();
        for(int i = 0; i < (edge+1)/2;i++){
            for(int j = 0; j < edge/2;j++){
                //matrix[i][j]->matrix[j][n-1-i]->matrix[n-1-i][n-1-j]->matrix[n-1-j][i]
                int temp = matrix[edge-1-j][i];
                matrix[edge-1-j][i] = matrix[edge-1-i][edge-1-j];
                matrix[edge-1-i][edge-1-j] = matrix[j][edge-1-i];
                matrix[j][edge-1-i] = matrix[i][j];
                matrix[i][j] = temp;
            }
        }
    }
};
```
## Result
Runtime: 0 ms, faster than **100.00%** of C++ online submissions for Rotate Image.
Memory Usage: 7.1 MB, less than **81.80%** of C++ online submissions for Rotate Image.