LeetCode: Rotate Image 解题报告

Rotate Image
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?
SOLUTION 1:
我们可以把它当作多个嵌套的环来处理,一环加一环。从外环处理到内环。为了方便处理各种下标,我们定义top, bottom, left, right来限制环的左右上下边界。
1. 令tmp = 上边界
2. 上边等于左边元素
3. 左边等于下边元素
4. 下边等于右边元素
5. 右边等于上边元素(tmp)
使用n来记录每一圈的边长。一圈进行4次操作,每次操作移动n - 1个元素即可。
public class Solution {
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0
|| matrix[0].length == 0) {
return;
}
int n = matrix.length;
int top = 0, down = n - 1, left = 0, right = n - 1;
while (n > 1) {
for (int i = 0; i < n - 1; i++) {
int tmp = matrix[top][left + i];
// 另上边等于左边
matrix[top][left + i] = matrix[down - i][left];
// 另左边等于下边
matrix[down - i][left] = matrix[down][right - i];
// 另下边等于右边
matrix[down][right - i] = matrix[top + i][right];
// 另右边等于上边
matrix[top + i][right] = tmp;
}
top++;
right--;
left++;
down--;
n -= 2;
}
return;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/Rotate.java
LeetCode: Rotate Image 解题报告的更多相关文章
- LeetCode: Rotate List 解题报告
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For exa ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】396. Rotate Function 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...
随机推荐
- dos下遍历目录和文件的代码(主要利用for命令)(转)
===== 文件夹结构 ============================================= D:\test ---A Folder 1 |-----A file 1.txt | ...
- 【基于Android的ARM汇编语言系列】之三:ARM汇编语言程序结构
作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell [ ...
- navicat ora-28547:connection to server failed
navicat ora-28547:connection to server failed CreationTime--2018年8月9日18点47分 Author:Marydon 1.情景还原 ...
- JNI入门
JNI是Java Native Interface的缩写,Native指C/C++. JNI内容涉及两个方面: Java调用C,这种情况是最主要的 C调用Java,这种情况不常见 第一步:编写Java ...
- 使用SoapUI生成WS请求报文
WSDL地址示例:http://10.1.84.10:8100/webService/common/mail?wsdl 打开SoapUI,创建一个Project,输入wsdl地址就ok. 1.访问 ...
- ubuntu(14.04) 安装ssh,并使用root用户登录
1.apt-get install openssh-server 2.修改ssh的配置文件/etc/ssh/sshd_config 注释掉以前的:PermitRootLogin without-pas ...
- 解决 无法将文件" "复制到“bin\*.*”。对路径“bin\*.*”的访问被拒绝。
关于这个问题今天碰到了, 我的解决办法很简单: 删除bin目录下的所有文件,然后重新生成,就不报错了.
- hdu 1280 前m大的数 哈希
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 软件测试自动化之- API Test
API测试 从本质上来说,API测试是用来验证组成软件的那些单个方法的正确性,而不是测试整个系统本身. API测试也被称为单元测试(Unit Testing), 模块测试(Module Testing ...
- java 随机日期
java 在某个范围日期内获取一个日期,再以这个日期作为开始日期,获取到随机n天后的日期 /** * 在beginDate和endDate之间获取一个随机日期作为开始日期 * @param begin ...