题目:

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. 植物大战僵尸中文第二版和年度版 游戏分析及delphi源码

    00413184 |. E8 77E30100   |CALL PlantsVs.00431500                  ; 地上的物品00413189 |. 8D7424 10     ...

  2. WPF学习笔记1——XAML之1

    参考文献: http://msdn.microsoft.com/zh-cn/library/ms752059(v=vs.110).aspx <Pro WPF 4.5 in C# > 一.X ...

  3. Ubuntu下编程环境GNU安装

    ubuntu下C编程   环境搭建 其实,linux下写C也是很容易的.IDE的话用 eclipse 集成 CDT 模块就行了.当然这属于重量级的了,就如同VC++之于windows一样.那有没有像T ...

  4. 含有自增序列的表中使用truncate与delete的不同结果

    一个含有自增序列的表,使用delete跟truncate之后会有什么不同结果呢? 大概说一下,使用truncate,表中的记录全部被清除,如果向表中插入数据,那么数据的排序是从1开始的. 如果使用的是 ...

  5. ubuntu c程序操作系统设备

    最近做一个局域网聊天系统,最后想操作系统播放音频文件.其实,Linux下的声音设备编程比大多数人想象的要简单得多.一般说来,我们常用的声音设备是内部扬声器和声卡,它们都对应/dev目录下的一个或多个设 ...

  6. android控件之EditText

    EditText继承关系:View-->TextView-->EditTextEditText的属性很多,这里介绍几个:android:hint="请输入数字!"//设 ...

  7. .NET序员的成长之路

  8. UML中的六种关系的比较与学习

    通过不断的学习并绘制UML图,整个画图的过程中深刻体会到其核心部分还是理解事物之间的关系,总结六大关系来深入学习,主要关系有六种:继承.实现.依赖.关联.聚合.组合. 区别于联系:         1 ...

  9. VBS基础篇 - class

    Class 语句:声明一个类的名称,以及组成该类的变量.属性和方法的定义. Class name '参数name必选项,Class 的名称 statements '一个或多个语句,定义了 Class ...

  10. c语言变量作用域问题

    c语言中的变量作用域总结 不管什么语言,main好像总是程序的入口,大括号是它的内容:变量的作用域总是困扰着我们,接下来,我们循序渐进的搞明白c语言中的变量作用域,首先得知道c是弱类型的语言,弱类型表 ...