现在的位置: 首页 > 综合 > 正文

LeetCode Rotate Image

2018年05月24日 ⁄ 综合 ⁄ 共 425字 ⁄ 字号 评论关闭

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

从外圈往里旋转

	void rotate(vector<vector<int> > &matrix) {
		int size = matrix.size();
		int left = 0, right = size-1; //从外往里,一圈圈的旋转
		vector<vector<int>> tmp;
		tmp = matrix;

		while(left < right)
		{
			for(int i = left,j = right; i < right+1,j>left-1; i++,j--)
			{
				matrix[left][i] = tmp[j][left];
				matrix[i][right] = tmp[left][i];
				matrix[right][i] = tmp[j][right];
				matrix[i][left] = tmp[right][i];
			}
			left++;
			right--;
		}
	}

抱歉!评论已关闭.