标签:
You are given an n x n 2D matrixrepresenting an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
HideTags
#pragma once
#include<iostream>
#include<vector>
using namespace std;
//[i,j]->[j,n-i]->[n-i,n-j]->[n-j,i]->[i,j] 一个环
void rotate(vector<vector<int> > &matrix)
{
int n = matrix.size();
//将矩阵四分之一(一角)进行此环操作
for (int i = 0; i < (n + 1) / 2; i++)//n为奇偶情况不一样,n=3:行01列0 n=4:行01列01
{
for (int j = 0; j < n / 2; j++)
{
int temp = matrix[i][j];//记录
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = temp;
}
}
}
void main()
{
system("pause");
}
标签:
原文地址:http://blog.csdn.net/hgqqtql/article/details/43767861