leetcode 随笔 Rotate Image

2人阅读 评论(0) 收藏 举报
分类:

一道找规律的题

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

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

Example 2:

Given input matrix =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

rotate the input matrix in-place such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]

可以看成是向右滚了一下矩阵

思路主要是遍历,矩阵的外层到内层滚动数字,每次移动4个数字。

只要想明白了,这个题还是很简单的。

 class Solution {
 public:
	 void rotate(vector<vector<int>>& matrix) {
		 int size = matrix.size();
		 if (size == 0 || size == 1) return;
		 digui(matrix, size, 0);
	 }
	 void digui(vector<vector<int>>& matrix, int size,int i)
	 {
		 if (size == 1)
			 return;
		 int zq = size - 1;
		 for (int j = i; j < zq+i; j++)
		 {
			 int value = matrix[i][j];
			 matrix[i][j] = matrix[i + i + zq - j][i];//top
			 matrix[i + zq + i - j][i] = matrix[i + zq][i + zq + i - j];//left
			 matrix[i + zq][i + zq + i - j] = matrix[j][i + zq];//bottom
			 matrix[j][i + zq] = value;//right
		 }
		 if (size == 2) return;
		 digui(matrix, size-2, i + 1);
	 }
 };

查看评论

LeetCode(48)Rotate Image

题目You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise)....
  • fly_yr
  • fly_yr
  • 2015-08-31 20:49:30
  • 2046

【LEETCODE】48-Rotate Image [Python]

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise)....
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016-01-19 17:30:17
  • 859

[LeetCode][Java] Rotate Image

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (...
  • Evan123mg
  • Evan123mg
  • 2015-07-14 19:57:09
  • 661

(Java)LeetCode-48. Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise...
  • u012848330
  • u012848330
  • 2016-09-29 15:36:17
  • 354

[leetcode-48]Rotate Image(java)

问题描述: You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockw...
  • zdavb
  • zdavb
  • 2015-08-02 21:59:36
  • 839

leetcode刷题系列C++-rotate image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise)....
  • u013676711
  • u013676711
  • 2016-02-22 11:18:26
  • 295

Rotate Image -- LeetCode

原题链接: http://oj.leetcode.com/problems/rotate-image/  这道题虽然操作起来有一点繁琐,但是思路比较简单,就是考察一下数组的基本操作。基本思路是把图片分...
  • linhuanmars
  • linhuanmars
  • 2014-03-21 10:32:48
  • 10054

[LeetCode]48.Rotate Image

【题目】 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (cloc...
  • SunnyYoona
  • SunnyYoona
  • 2014-01-21 21:56:31
  • 2674

[C语言][LeetCode][48]Rotate Image

题目Rotate Image You are given an n x n 2D matrix representing an image.Rotate the image by 90 degree...
  • Timsley
  • Timsley
  • 2015-08-28 23:25:45
  • 1193

leetcode之图像旋转(Rotate Image)

新建一个数组,将原数组的数据按规律复制到新数组,这种方法做不到in-place,占用了额外一个数组的空间 newx = y; newy = n-1-x; 我们可以按ring by ring...
  • u011388550
  • u011388550
  • 2016-10-10 16:29:24
  • 424
    个人资料
    等级:
    访问量: 220
    积分: 233
    排名: 31万+
    文章分类
    文章存档