首页 > > > [LeetCode] Rotate Image
Rotate Image
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?
解题思路:

题目要求原地算法。假设坐标轴如上所示。顺时针旋转90度,相当于先将图片沿着x=n/2翻转,然后再将图片沿着斜对角线y=x翻转。代码如下:
class Solution {
public:
void rotate(vector>& matrix) {
int len = matrix.size();
//先将矩阵左右翻转
for(int i = 0; i& v){
int len = v.size();
int i=0, j=len-1;
while(i& v, int i, int j){
int temp = v[i];
v[i] = v[j];
v[j] = temp;
}
};
原文作者:;如有侵权,请联系管理员删除。