[抄题]:

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. 360插件化Replugin爬坑之路

    前言 继上次爬完了热修复的坑位,中途爬了各种各样的坑.今天我们来说说插件化Replugin的坑位.Replugin刚出的时候我就看过了.第一次看的时候可能心态不好.没看懂= =第二次重头在看,发现蛮简 ...

  2. adb命令安装apk

    1.将需要安装的apk放在platform-tools下 2.将手机和电脑连接,在cmd中输入 adb devices查看 3.使用adb命令安装apk,在cmd中输入:adb install apk ...

  3. 这是C语言结课前(期末考试之前)写给牛晓霞的一封信!

    致尊敬的牛晓霞老师: 这是黄领衫的感想,也是想告诉你的话! 在老师说要给班里写得好的人发黄领衫的时候,我当时的想法是我很有可能拿到这份奖品的,怎么说呢,算是一种自信吧,或是对自己的态度的认可.虽然我能 ...

  4. php中require_once与include_once的区别

    首先include_once仅包含文件一次,如果没有文件,会发出警告,并继续执行. 而require_once也是仅包含文件一次,但是如果程序中没有找到文件,则程序会中止执行.

  5. jenkins配置email

    # 系统设置 # Jenkins Location # 邮件通知 # 高级 # Failed to send out e-mail 勾选“使用SSL协议” SMTP端口改为465 密码使用授权码,不能 ...

  6. .NET IL指令速查表

    名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...

  7. timescaledb replication 使用

    replication 可以确保系统的ha 以及lb 数据的查询,timesacledb 使用pg 内置的stream replication 进行复制的支持 docker 运行参考 https:// ...

  8. 记 TP-Link 路由器的 WDS 设置

    记 TP-Link 路由器的 WDS 设置 有一台旧的 TP-Link 路由器,是以前朋友送的,外壳看起来还不错,也挺新的. 本来已经有一台极路由了,看到信号还是不够好,所以想使用 TP-Link 的 ...

  9. Phonegap 通知 Notification

    通知 Notification 一.notification.alert 对话框 notification.alert 响铃 notification.beep 震动 notification.vib ...

  10. ORA-01652: 无法通过 128 (在表空间 TEMP 中) 扩展 temp 段(EXP-00056: 遇到 ORACLE 错误 1652 ORA-01652: unable to extend temp segment by 128 in tablespace TEMP)

    数据库报 ORA-01652: 无法通过 128 (在表空间 TEMP 中) 扩展 temp 段 两种解决方式: 第一种) sql>select * from v$tempfile; 发现tem ...