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. Python 死循环和嵌套循环

    何为死循环:在编程中,一个无法靠自身的控制终止的循环被称为死循环. 死循环的使用:死循环并非一无是处,C语言中死循环while true或 while 1 是单片机编程的普遍用法,死循环一直运行等待中 ...

  2. POJ 3007 Organize Your Train part II (字典树 静态)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6478   Acce ...

  3. Android WebDriver 浏览器自动测试工具介绍

    Selenium WebDriver 是浏览器自动测试工具,提供轻量级和优雅的方式来测试web应用.Selenium WebDriver作为Android SDK extra,支持Android 2. ...

  4. C# 发布REST接口地址API服务

    原文地址:https://blog.csdn.net/chinacsharper/article/details/21256569 今天碰巧,用到了淘宝的在线IP地址查询的Rest API,它提供接口 ...

  5. Flume多Sink方案修正

    在实际项目中采用http://www.cnblogs.com/moonandstar08/p/6091384.html方案进行布署时,由于系统产生的消费比较大按照原方案进行布署时,随着国外局点不断增加 ...

  6. ExpandoObject与DynamicObject的使用

    using ImpromptuInterface; using System; using System.Dynamic; namespace ConsoleApp2 { class Program ...

  7. android LinearLayout设置selector不起作用解决

    设置方法 : android:background="@drawable/fen_selector" 如果只有这个的话,是不起作用的.还必须加上: android:clickabl ...

  8. 转 selenium 自动下载文件

    #coding=utf-8from selenium import webdriver #实例化一个火狐配置文件fp = webdriver.FirefoxProfile() #设置各项参数,参数可以 ...

  9. Linux内核配置解析 - Boot options

    1. 前言 本文将介绍ARM64架构下,Linux kernel和启动有关的配置项. 注1:本系列文章使用的Linux kernel版本是“X Project”所用的“Linux 4.6-rc5”,具 ...

  10. oracle Database link 创建

    http://www.cnblogs.com/yhason/p/3735319.html