旋转图像 · Rotate Image
[抄题]:
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的更多相关文章
- [Swift]LeetCode48. 旋转图像 | Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 爬虫(十三):PIL模块
1. PIL模块 在爬虫(十二):图形验证码的识别.滑动验证码的识别(B站滑动验证码)中我留下了一个悬念,为什么安装的是pillow模块,而不是PIL模块.这是因为PIL是python2的产物,它并没 ...
- AI换脸实战教学(FaceSwap的使用)---------第一步Extration:提取人脸。
市面上有多款AI换脸的方法,笔者这里节选了Github那年很火的开源项目FaceSwap: (很早就实践了,但是忘记记录啦hhh,请勿用于不正当用途哦) 做了一篇详细教学,包括配置,参数设置,换脸效果 ...
- 048 Rotate Image 旋转图像
给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [ [1,2,3], [4 ...
- LeetCode 48. 旋转图像(Rotate Image)
题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: ...
- Leetcode48. Rotate Image旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 记一道有意思的算法题Rotate Image(旋转图像)
题出自https://leetcode.com/problems/rotate-image/ 内容为: You are given an n x n 2D matrix representing an ...
随机推荐
- self-taught learning setting && semi-supervised learning
参考文献: 摘于上文献: The more general and powerful setting is the self-taught learning setting, which does n ...
- bzoj 4177 Mike的农场
bzoj 4177 Mike的农场 思维有些江化了,一上来就想费用流做法,但其实就是个最小割啊. 考虑先将所有的收益拿到,再减去不能拿的以及三元组 \((i,j,k)\) 产生的代价.即,先让 \(a ...
- iOS NSLog去掉时间戳及其他输出样式
1.一般项目中我的NSLog会在Prefix.pch文件添加如下代码,已保证在非调试状态下NSLog不工作 1 2 3 4 5 #ifdef DEBUG #define NSLog(...) NS ...
- Swift学习——A Swift Tour 协议和扩展
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/28854395 Protocols an ...
- 打造html右键菜单
今天是给大家介绍一款在网页上使用的右键菜单,原作者的网址是:http://51jsr.javaeye.com/blog/305517 这个右键菜单已经非常优秀,不过呢.却是IE Only,而且在DTD ...
- sysstat工具
sysstat工具可以监控系统的IO,CPU,SWAP,LOAD,NETWORK,DISK 安装后,系统会生成定时任务脚本 路径:/etc/cron.d/sysstat 内容: # Run syste ...
- Phonegap 原生控件(Android)与html混合
1. 用命令创建cordova项目 cordova coreate hello com.example.hello hello 2.打开MainActivity 在onCreate方法中加入 setC ...
- (转)Eclipse新增安卓虚拟机
- hdu1 247 Hat’s Words(字典树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- PHP-fpm启动时 出现 PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) 'fileinfo.so' in Unknown on line 0
出现该问题的原因之一是: 在编译PHP时启用了fileinfo扩展(内置),但同时在php.ini文件中添加了: extension=fileinfo.so 去掉或注释之后,重启php-fpm,警告消 ...