旋转图像 · 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 ...
随机推荐
- Relation.js——基于pixi.js的拓展模块之人物关系图谱
出于[重构基于D3的关系图谱项目]的目的,在看完pixi.js之后,并且网上又没有现成的基于webgl的关系图谱js库,于是,本人决定自己写一个. 因为平常要工作的原因,进度可能有点慢,但是githu ...
- Servlet不是线程安全的。
要解释为什么Servlet为什么不是线程安全的,需要了解Servlet容器(即Tomcat)使如何响应HTTP请求的. 当Tomcat接收到Client的HTTP请求时,Tomcat从线程池中取出一个 ...
- 新转移注意(caffe):ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory
https://github.com/NVIDIA/DIGITS/issues/8 For this error ImportError: libcudart.so.7.0: cannot open ...
- Jmeter-线程组执行顺序控制
线程组按顺序来执行,大概思路, 1.需要控制线程组内的操作在满足某一条件才执行,那么就需要使用if或者while: 2.要使用if或者while都需要一个变量,而这个变量要在两个或多个线程组内使用,那 ...
- Linux中文档去掉windows文本的多余的回车符(^M)
1) 使用sed 去掉windows下的回车符 (注意^M 在linux 下写法 按^M 是回车换行符,输入方法是按住CTRL+v,松开v,按m) sed -i 's/^M//g' filenam ...
- python 用 __all__ 暴露接口
非常典型的python的用法 refer to : http://python-china.org/t/725
- 项目管理软件选择:redmine or JIRA
个人理解,这两款软件从本质上说是issue tracking,而不是项目管理. 先说些个人的想法 1)从现阶段情况看,都是够用的,毕竟本来就是小团队 2)从扩展而言,根据现在团队的实际情况(基本都是搞 ...
- SublimeText设置在浏览器打开 快捷键
这里插入一下安装"view in browser"官方版的说明:(前提是得先安装package control插件) 1.通过"ctrl+shift+p"打开命 ...
- ThinkPHP think-migration 简洁使用教程
ThinkPHP think-migration 简洁使用教程 migration:一种数据库的版本控制,让团队在修改数据库结构的同时,保持彼此的进度一致.帮你更简单的管理数据库.基于原生 think ...
- 定时器Timer&ScheduledThreadPoolExecutor
定时器Timer&ScheduledThreadPoolExecutor /** * @ClassName: TimerTest * @author: daniel.zhao * @date: ...