# 0048. Rotate Image ###### tags: `Leetcode` `Medium` Link: https://leetcode.com/problems/rotate-image/ ## 思路 四个为一组换一圈   注意找起始点的方法是**取最左上的起始点**,例如上面$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.
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up