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 解题报告的更多相关文章

  1. LeetCode: Rotate List 解题报告

    Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For exa ...

  2. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  6. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  7. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  8. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】396. Rotate Function 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...

随机推荐

  1. 使用 C# 开发智能手机软件:推箱子(十二)

    这是"使用 C# 开发智能手机软件:推箱子"系列文章的第十二篇.在这篇文章中,介绍 Window/AboutDlg.cs 源程序文件. 这个源程序文件包括 AboutDlg 类,该 ...

  2. c语言转移符和三字母序列

    三字母序列

  3. Spring(十二)使用Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...

  4. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口(转)

    转自:[CXF REST标准实战系列] 二.Spring4.0 整合 CXF3.0,实现测试接口 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现W ...

  5. Fork me on GitHub

    <a href="https://github.com/yadongliang"><img style="position: absolute; top ...

  6. cxf之Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cxf.jaxrs.provider.ProviderFactory.<init>(Lorg/apache/cxf/Bus;)V

    pom.xml中关于cxf的配置jar包冲突??? 1.http://blog.csdn.net/yzl_8877/article/details/53216923 2.https://www.cnb ...

  7. IDEA+SpringMVC+Spring+Mybatis

    详细参照: SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 这里只说一下注意的地方: 1.上面那篇是用的eclipse, 但IDEA的目录结构和eclipse稍有不同. ...

  8. HDUOJ-----取(m堆)石子游戏

    取(m堆)石子游戏 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  9. Putty设置删除

    1. 开始→运行→cmd(进入字符界面) 2.切换目录到putty安装目录 3.执行以下清理命令 4.会跳出如下提示信息 点击[是(Y)]

  10. SYS_R12 MOAC多组织底层技术实现技术分析(Oracle VPD) (案例)

    2014-05-30 Created By BaoXinjian