题目:

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?

链接:  http://leetcode.com/problems/rotate-image/

题解:

in-place,按照顺时针移动整个矩阵的1/4,注意边长为奇数时的corner case。 还有方法是对折再变换的。

Time complexity - O(n2), Space Complexity - O(1)

public class Solution {
public void rotate(int[][] matrix) {
if(matrix == null || matrix.length == 0)
return;
int n = matrix.length - 1; for(int i = 0; i <= n / 2; i++) {
for(int j = 0; j < (n + 1) / 2; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - j][i];
matrix[n - j][i] = matrix[n - i][n - j];
matrix[n - i][n - j] = matrix[j][n - i];
matrix[j][n - i] = tmp;
}
}
}
}

Follow up是对矩阵最外部的元素,每个元素向右移动一个位置。可能药用spiral matrix一类的方法。

再Follow up可以 design一个三消游戏,类似 Candy Crush saga。

二刷:

这里看漏了n x n matrix,其实m = n就可以了。注意遍历的边界条件, i 可以 < n /2, 这样j就必须要 j < (n + 1) / 2来cover中间的奇数元素。还可以想得再透彻一些。还有不少大神有很好的翻转方法,先记录在reference里,留给三刷了。

Java:

Time complexity - O(n2), Space Complexity - O(1)

public class Solution {
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0) {
return;
}
int m = matrix.length, n = matrix[0].length;
for (int i = 0; i < m / 2; i++) {
for (int j = 0; j < (n + 1) / 2; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[m - 1 - j][i];
matrix[m - 1 - j][i] = matrix[m - 1 - i][n - 1 - j];
matrix[m - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = tmp;
}
}
}
}

同时也做了一下 Rotate Matrix by 1, 发了一篇新文

Rotate Matrix by One

题外话:

1/27/2016

今天群里讨论interval search tree,很精彩。我打算好好复习一下相关的知识。昨天讨论Google面试题密码箱问题,de brujin, hamiton path和 euler path,还是要好好向dietpepsi大神学习。 要扎实,反应敏捷并且准确,还要能迅速写出代码才能。现在差得还很远。

三刷:

这里的边界条件要注意一下。因为题目给出m = n,所以input是边长都是n的方阵。 我们旋转的方阵分为两种,一种是边长为奇数的,一种是边长为偶数的。

旋转边长为偶数的矩阵时,我们要根据  len / 2 把矩阵分为四个部分,只需要旋转左上部分就行了,比如n = 4,那么我们的条件就是 i < 2和 j < 2。

[1, 2, 3, 4]

[5, 6, 7, 8]

[1, 2, 3, 4]

[5, 6, 7, 8]

另外一种是奇数边长的矩阵,这时候我们旋转的也是左上部分,不过左上部分这时候不是一个方阵,而是一个类似于三角形,或者梯形的区域。比如下图,这里其实我们要旋转的是1和2,或者1和4。假如我们同时旋转1, 2和4, 那么就会出现重复步骤造成结果不正确。

所以这时候我们对边长的界定应该是,  i < (len + 1) / 2, 即对row 添加1然后除以2,转换成偶数边长时遍历的情况, 而 j < len / 2, 对j来说不变,还是维持奇数边长时遍历的情况。当然这里我们也可以互换i 和 j。 这样处理就保证了我们在操作的时候没有多余步骤。

[1, 2, 3]

[4, 5, 6]

[7, 8, 9]

Java:

public class Solution {
public void rotate(int[][] matrix) {
if (matrix == null) return;
int len = matrix.length;
for (int i = 0; i < (len + 1) / 2; i++) {
for (int j = 0; j < len / 2; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[len - 1 - j][i];
matrix[len - 1 - j][i] = matrix[len - 1 - i][len - 1 - j];
matrix[len - 1 - i][len - 1 - j] = matrix[j][len - 1 - i];
matrix[j][len - 1 - i] = tmp;
}
}
}
}

Reference:

https://leetcode.com/discuss/20589/a-common-method-to-rotate-the-image

https://leetcode.com/discuss/38426/seven-short-solutions-1-to-7-lines

https://leetcode.com/discuss/27262/java-in-place-solution-with-explanation-easy-to-understand

48. Rotate Image的更多相关文章

  1. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  2. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  3. 刷题48. Rotate Image

    一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...

  4. 48. Rotate Image - LeetCode

    Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...

  5. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  7. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  8. LeetCode OJ 48. Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. AIR串口通信

    最近公司的项目中需要用到串口通信,项目是用基于AIR的,AIR本身是不支持串口通信的,本想用 c#或java另写一个负责串口通信的模块,又感觉很烦不想那么弄,就想到了ANE.可惜以前也没弄过 ANE, ...

  2. 第一个C#应用 【搜索软件】

    搜索软件V1.0 [附软件截图][http://pan.baidu.com/s/1mihEbe4] 设备搜索:支持广播搜索[local search],指定ip[range search]搜索,直接w ...

  3. DBus通讯

    linux下进程间通信的方式主要有Pipe(管道),FIFO(命名管道),信号,共享内存,消息队列,信号灯等,这些方式各有 各得特点,如管道是linux下命令行中常用的,用于父子进程的通信.但是这些通 ...

  4. 修改mysql的root密码

    use msyql; update user set password=password('新密码') where user='root'; flush privileges; quit net st ...

  5. iOS 进阶 第十天(0410)

    0410 在tableViewCell之间添加一根线,通栏 iOS应用数据存储的常用方式 plist存储文件 plist读取文件 下面是plist存储读取的图解: 注意:plist只能存储常见的属性. ...

  6. 【学习总结】iOS 数据保存几种方式总结

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: NSKeyedAr ...

  7. SqlServer里DateTime转字符串

    Select CONVERT(varchar(100), GETDATE(), 8):14:53:14 Select CONVERT(varchar(100), GETDATE(), 9): 06  ...

  8. python学习小结6:模块

    模块:在Python中有一个概念叫做模块(module),简单地说,模块就是一个保存了Python代码的文件.          模块能定义函数,类和变量,模块里也能包含可执行的代码.         ...

  9. .net datatable 添加一列

    dt.Columns.Add("image", Type.GetType("System.String")); foreach (DataRow dr in d ...

  10. C#WinForm中显示实时时间:年/月/日 时/分/秒 星期X

    //加载窗体时 string weekstr = ""; private void Form22_Load(object sender, EventArgs e) { this.t ...