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 ...
随机推荐
- 简单的 Helper 封装 -- CookieHelper
using System; using System.Web; namespace ConsoleApplication5 { /// <summary> /// Cookie 助手 // ...
- Adaptive Thresholding & Otsu’s Binarization
Adaptive Thresholding Adaptive Method - It decides how thresholding value is calculated. cv2.ADAPTIV ...
- 微软 Visual Studio 2017 中文正式版下载 – 免费社区版/专业版/企业版
作为“宇宙最强”的集成开发环境 IDE,微软的 Visual Studio 不仅破天荒发布了 macOS 版本,如今终于也推出了其 Windows 的最新版本—— VS 2017 正式版了.这对开发者 ...
- highstock高级篇之股票分时图
一直在用 highchart 在做图表,最近一段时间突然接到一活,需要用 highstock 帮客户完成一个股票K线图和分时图.虽然用法和 api上与 highchart 没什么区别,但还是研究一番做 ...
- HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again
Calling Extraterrestrial Intelligence Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- Java数据库表自动转化为PO对象
本程序简单实现了数据库内省,生成PO对象. 数据库内省有如下两种实现方式: 通过mysql元表 通过desc table,show tables等命令 import java.io.IOExcepti ...
- 2014年15款新评定的最佳PHP框架
通常,框架都会被认为是帮助开发者快速设计和开发动态网站的软件应用.每个月都有极大数量的新发布的 PHP 框架,使网站开发更简单更高效. 如果你是位 PHP 开发者,正在寻找当前最好的一些 PHP 框架 ...
- SAP接口设计的扩展性考虑
由于现在的系统和SAP的接口出现了几次变更,因此需要对系统进行设计改造.由于系统中和SAP交互的接口不止一处,而且也是在不同的时间段进行开发,并由不同的人员来完成的,因此我在维护升级的 ...
- Android oncreate onupgrade什么时候被调用
在学习Android数据库SQLite之前,必须意识到这一点,目前在Android系统中集成的是SQLite3 版本,SQLite是一个开源的嵌入式数据库,他支持NULL.INTEGER.REAL.T ...
- Python 字典 in 操作符
描述 Python 字典 in 操作符用于判断键(key)是否存在于字典(D)中,如果键在字典中返回True,否则返回False. 在Python2中还可以使用 has_key() 方法,官方文档推荐 ...