码迷,mamicode.com
首页 > 其他好文 > 详细

48.Rotate Image

时间:2015-02-12 22:52:50      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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

 Array


#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");
}



48.Rotate Image

标签:

原文地址:http://blog.csdn.net/hgqqtql/article/details/43767861

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!