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. POJ-3045 Cow Acrobats (C++ 贪心)

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  2. JAVA设计模式:蝇量模式

    声明:转载请说明来源:http://www.cnblogs.com/pony1223/p/7554686.html 一.引出蝇量模式 现在假设有一个项目,这个项目是为公园设计一个景观的部署,那么这个时 ...

  3. jquery-easyUI第二篇【综合案例】

    基于easyUI开发的一个综合案例模版 <%@ page language="java" pageEncoding="UTF-8"%> <!D ...

  4. linux(5)--补充(管道| / 重定向> / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)

    本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下一. 管道| / 重定向> / xargs 如:1. 管道和重定向的区别:具体可以见 http://www. ...

  5. Linux tomcat 去除项目名端口号直接用ip或者域名访问网站

    网站开发过程中,一般的工程访问路径是  http://10.10.10.10:8080/projectName如何设置成http://10.10.10.10/ 解决方法: 首先,进入tomcat的安装 ...

  6. 在 Ubuntu 上安装 MongoDB

    在 Ubuntu 上安装 MongoDB 运行下列命令,导入 MongoDB 公开 GPG 键: sudo apt-key adv --keyserver hkp://keyserver.ubuntu ...

  7. Go语言备忘录:基本数据结构

    本文内容是本人对Go语言的变量.常量.数组.切片.映射.结构体的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处,多谢! 参考书籍<Go语 ...

  8. Oculus关于Internal Error:OVR53225466报错解决方法

    安装Oculus过程中可能会出现Internal Error:OVR53225466报错提示,如附件所示: 解决方法:修改hosts文件 操作方法: (1)以管理员方式打开记事本: (2)打开C:\W ...

  9. JS数字金额转换为货币汉字形式

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  10. 705. New Distinct Substrings spoj(后缀数组求所有不同子串)

    705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...