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?


题目标签:Array

  这道题目给了我们一个n * n的矩阵,让我们旋转图片。题目要求in-place,所以就不能用额外的空间了。一开始自己写了一个很原始的方法,结果虽然通过,但是速度太慢。只好去看看别人的方法,看完觉得,自己以前数学课学的东西都还给老师了。来分析一下题目,举一个例子

2  3           1    7           7  4  1

4    6           2    8           8  5  2

7  8             3    9           9  6  3

第一步,根据红色的对角线,找对应位置,互换两个数字的值。

第二步,对每一行数字,根据中线左右翻转。

Java Solution:

Runtime beats 61.89%

完成日期:07/17/2017

关键词:Array

关键点:先逆矩阵再根据中线左右翻转每一行

 public class Solution
{
public void rotate(int[][] matrix)
{
int n = matrix.length; // along the left top to right bottom diagonal line, swap symmetrical pair
for(int i=0; i<n; i++) // for each row
{
for(int j=i+1; j<n; j++) // for each number
{
// swap the pair
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
} // flip each row horizontally
for(int i=0; i<n; i++)
{
for(int j=0; j<n/2; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n-1-j];
matrix[i][n-1-j] = temp; }
}
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4389572.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 48. Rotate Image(旋转图像)的更多相关文章

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

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

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

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

  3. [leetcode]48. Rotate Image旋转图像

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

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

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

  5. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  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 My Submissions Question (矩阵旋转)

    题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...

  8. LeetCode 48. Rotate Image (C++)

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

  9. 【刷题笔记】LeetCode 48. Rotate Image

    题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...

随机推荐

  1. logback:logback和slf4j中的:appender、logger、encoder、layout

    (1)appender 1.appender标签是logback配置文件中重要的组件之一.在logback配置文件中使用appender标签进行定义.可 以包含0个或多个appender标签. 2.a ...

  2. Spring第七篇【Spring的JDBC模块】

    前言 上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的对JDBC的支持- 对于JDBC而言,我们肯定不会陌生,我们在初学的时候肯定写过非常非常多的JD ...

  3. 02_Ext_Panel

    1,面板由以下几个部分组成, 一个顶部工具栏(tbar).一个底部工具栏(bbar).面板头部(header).面板尾部(bottom).面板主区域(body)几个部分组成. 面板类中还内置了面板展开 ...

  4. 关于XCode9的一些问题

    最近几天一直在做一项工作,为新项目在做搭建框架的前期准备,然后恰逢更新iOS11和XCode9,笔者心急的尝了个先,发现了一些问题,记录一下,如果有相同问题的程序猿,也可以参考一下. 目前问题遇到两点 ...

  5. LCA问题第二弹

    LCA问题第二弹 上次用二分的方法给大家分享了对 LCA 问题的处理,各位应该还能回忆起来上次的方法是由子节点向根节点(自下而上)的处理,平时我们遇到的很多问题都是正向思维处理困难而逆向思维处理比较容 ...

  6. session写入memcache

    1 <?php 2 class MemSession{ 3 private static $handler = null; 4 private static $lifetime = null; ...

  7. Python文件读写模式

    r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在.可读,可写,可追加. w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失.若文件不存在则建立该文件. w+ 打 ...

  8. CSS div阴影效果

    <div class="image"><img src="default.jpg" /></div> .image{box- ...

  9. IIS7中JS、CSS、Image无法显示和加载解决方案

    前两天把机器从Windows7升级到Windows10,IIS也跟着升级了,在获取项目搭载IIS上发现原有的页面中所有的JS.CSS.Image都无法访问,提示500错误,起初以为是IIS没有装好 重 ...

  10. Python自学笔记-paramiko模块(Mr serven)

    文章出处:http://www.cnblogs.com/wupeiqi/articles/5095821.html SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: #!/u ...