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 ...
随机推荐
- 怎样以Root方式执行Xcode
粗略算一下,在第一次接触OSX的时候,我接触Windows已经有14年,刚開始用OSX和Xcode各种不习惯.可是用Xcode写了一星期的代码,我却有一种想把Windows和VS扔了的感觉(真的用着非 ...
- Cocos2d-x3.0 iOS 一键编译多个target并打包ipa。
1.编写app打包为ipa的 shell脚本.将以下代码保存为app2ipa.sh. #!/bin/sh m_appPath="" m_ipaPath="" m ...
- c# 获取北京时间更新本地计算机
class UpdateDateTime { [DllImport("Kernel32.dll")] private static extern void SetLocalTime ...
- springmvc json结合
获取json数据 名字一样就获取了 user @RequestMapping("/addUser") public String addUser(User user,Htt ...
- linux创建新用户及权限
在Linux中添加普通新用户 ,超级用户(也称为“root”)是一个具有修改系统中任何文件权力的特别账号.在日常工作中,最好不要使用超级用户账号进入系统,因为任何错误操作都可能导致巨大的损失.由于超级 ...
- ubuntu 安装 LAMP环境
http://jingyan.baidu.com/article/a681b0de36ad683b18434691.html
- JQuery中事件one、bind、unbind、live、delegate、on、off、trigger、triggerHandler的各种使用区别
JQuery事件one,支持参数 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> & ...
- js时间戳转成日期格式
将时间戳转换成日期格式:// 简单的一句代码var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得的时间戳 ...
- pylot 学习笔记
安装步骤 1.下载pylot 版本是1.26,文件名是:pylot_1.26.zip 2.下载python 版本是2.5,文件名是:python-2.5.msi 3.下载numpy 版本是1.4.1, ...
- DevExpress.XtraReports:XRPivotGrid 笔记
1. DevExpress.XtraReports:XrPivotGrid 显示时间为"0"的 格式问题: 把xrPivotGridField1的SummaryType改为&quo ...