[抄题]:

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]
]

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

先xy翻转,再对折。就差一步了,观察力不够没看出来

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

i代表行的坐标,j代表列的坐标,j = i时代表xy

[复杂度]:Time complexity: O(m*n) Space complexity: O(m*n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

289. Game of Life 题号小的很多题,就是一般的数组变换

[代码风格] :

public class Solution {
/*
* @param matrix: a lists of integers
* @return:
*/
public void rotate(int[][] matrix) {
//corner case
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return ;
}
int length = matrix[0].length;
//reverse
for (int i = 0; i < matrix.length; i++) {
for (int j = i; j < matrix[0].length; j++) {//xy
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
//flip
for (int i = 0; i < matrix.length; i++) {//all row
for (int j = 0; j < matrix[0].length / 2; j++) {// half col
int temp = matrix[i][j];
matrix[i][j] = matrix[i][length - 1 - j];
matrix[i][length - 1 - j] = temp;
}
}
}
}

旋转图像 · Rotate Image的更多相关文章

  1. [Swift]LeetCode48. 旋转图像 | Rotate Image

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

  2. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  3. 爬虫(十三):PIL模块

    1. PIL模块 在爬虫(十二):图形验证码的识别.滑动验证码的识别(B站滑动验证码)中我留下了一个悬念,为什么安装的是pillow模块,而不是PIL模块.这是因为PIL是python2的产物,它并没 ...

  4. AI换脸实战教学(FaceSwap的使用)---------第一步Extration:提取人脸。

    市面上有多款AI换脸的方法,笔者这里节选了Github那年很火的开源项目FaceSwap: (很早就实践了,但是忘记记录啦hhh,请勿用于不正当用途哦) 做了一篇详细教学,包括配置,参数设置,换脸效果 ...

  5. 048 Rotate Image 旋转图像

    给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [  [1,2,3],  [4 ...

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

    题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: ...

  7. Leetcode48. Rotate Image旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

  8. [LeetCode] Rotate Image 旋转图像

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

  9. 记一道有意思的算法题Rotate Image(旋转图像)

    题出自https://leetcode.com/problems/rotate-image/ 内容为: You are given an n x n 2D matrix representing an ...

随机推荐

  1. Linux:提示符PS1个性设置

    提示符PS1个性设置 1)默认PS1 echo $PS1 2)个性PS1 #去掉了默认显示的[]号#\e[1;34m\]\u:user名高亮显示并显示颜色#\e[5;33m\]\h:hostname主 ...

  2. cetos7 systemd 详解

      CentOS7/RHEL7 systemd详解 目录1. 为什么是systemd(1) 关于Linux服务管理(2) SysV init的优缺点(3) UpStart的改进(4) systemd的 ...

  3. 本地代码同步到github

    1 设置 ssh 公钥信息 首先你要确保 github 账号设置了ssh 公钥信息.如果没有的话可以按照下面的方式设置: 前往 github 网站的 account settings, 依次点击 Se ...

  4. 为什么有logistics函数

    直观地看: 如果是softmax函数,我想有跟多的选择方向吧

  5. Hibernate对象的三种状态,瞬时态、持久态、游离态

    1.瞬时态.(new完一个对象,突然断电,内存中没有此对象) hibernate中什么时候的对象为瞬时态呢,当我们new 一个对象时,还没有save时,它就是瞬时态的,当我们delete一个对象时,它 ...

  6. Redis学习笔记-安装篇(Centos7)

    1.安装 这里使用源代码安装的方式,如果你希望使用yum或者rpm包安装的方式,可以百度一下,安装方法可谓多如牛毛. # 下载安装包 # wget http://download.redis.io/r ...

  7. ajax请求返回Json字符串运用highcharts数据图表展现数据

    [1].[图片] Json字符串和highcharts数据图表展现.jpg 跳至 [1] code=26754#44745" rel="nofollow"> [2] ...

  8. 【转】MFC String处理

    原文网址:http://www.cnblogs.com/lisuyun/p/3399232.html C/C++获取文件后缀名并且比较 以下这段是VC中过去文件后缀名的方法 1.CString Get ...

  9. VS2010 C++环境下DLL和LIB文件的生成与调试 备忘

    利用VS2010工具,调试DLL文件的方法现总结如下: 在一个解决方案中生成两个工程,假设MYDLL和MYDLG两个工程,前者是DLL工程,后者DLG调用前边的DLL工程.设置如下: 目录如下:图,本 ...

  10. java课程设计-坦克大战

    团队课程设计博客链接 个人负责模块 枚举类.工具类.子弹类.图片素材的查找,地图制作 Git管理 包名类名的命名 详细说明 枚举类 如 单人和双人模式 工具类 将每个图片的路径使用字符串保存,便于调用 ...