题目在这里 https://leetcode.com/problems/rotate-image/

【个人分析】

  这个题目,我觉得就是考察基本功、考察细心的,算法方面没有太多东西,但是对于坐标的使用有较高要求。

放了两个版本的答案,第一个版本是自己写的,第二个是目前最佳答案的Java改写。

【代码注释】

Solution1: 思路比较直接。既然要求in-place,那就在修改之前,先保存原先在那个位置上的值,然后尾巴咬尾巴的去修改;大意可以参考下图

Solution2: 偏数学的方法,先把矩阵“上下对调”: 第一行和最后一行换,第二行和倒数第二行换。然后再镜像交换,第 i 行第 j 列的数和第 j 行第 i 列的数字交换。

 public class Solution {
/**
* 1 2 3 4 13 9 5 1 13 9 5 1
* 5 6 7 8 outer loop 14 6 7 2 inner loop 14 10 6 2
* 9 10 11 12 ============> 15 10 11 3 ============> 15 11 7 3
* 13 14 15 16 16 12 8 4 16 12 8 4
* @param matrix
*/
public void rotate(int[][] matrix) {
int n = matrix.length;
if (n == 0) {
return;
}
int half = n / 2;
// for each loop
for (int i = 0; i < half; i++) {
int startIndex = i;
int endIndex = startIndex + (n - 2 * i) - 1;
// in one row, we leave the last number unchanged
// so it is j < endIndex, not j <= endIndex
for (int offset = 0; startIndex + offset < endIndex ; offset++) {
// number in the first row
int temp1 = matrix[startIndex][startIndex + offset];
// number in the last column
int temp2 = matrix[startIndex + offset][endIndex];
// number in the last row
int temp3 = matrix[endIndex][endIndex - offset];
// number in the first column
int temp4 = matrix[endIndex - offset][startIndex]; matrix[startIndex][startIndex + offset] = temp4;
matrix[startIndex + offset][endIndex] = temp1;
matrix[endIndex][endIndex - offset] = temp2;
matrix[endIndex - offset][startIndex] = temp3; }
} }
}

Solution2:

 public class Solution {
/**
* First reverse top-bottom, then reverse symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 ==> 4 5 6 ==> 8 5 2
* 7 8 9 1 2 3 9 6 3
* @param matrix
*/
public void rotate(int[][] matrix) {
int n = matrix.length;
int middle = n / 2;
// reverse top-bottom, swap the ith row with (n-i)th row
for (int i = 0; i < middle; i++) {
for (int j = 0; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - i][j];
matrix[n - 1 - i][j] = temp;
}
} // swap symmetry
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
} } } }

[Leetcode][048] Rotate Image 略详细 (Java)的更多相关文章

  1. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

  2. Java for LeetCode 048 Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  3. LeetCode 048 Rotate Image

    题目要求:Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 deg ...

  4. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  5. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  6. Java for LeetCode 189 Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  7. Java for LeetCode 061 Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...

  8. Java [Leetcode 189]Rotate Array

    题目描述: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...

  9. 【LeetCode】048. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

随机推荐

  1. 简单代码JS随机,数组操作,延时执行

    <script language="javascript"> var str="baidu.com,163.com,sohu.com,kankan.com,1 ...

  2. Delphi XE7下 Intraweb 发布为ASP.NET应用程序

    一.XE7下Intraweb开发这里就不说了,重点是在开发Intraweb时与ISAPI不同之处要选择 IW library,编译成DLL文件. 二.网站的配置 1.这是站点的物理路径,c:\site ...

  3. Python中else语句块(和if、while、for、try搭配使用)

    学过C/C++的都知道,else语句是和if语句搭配使用的, 但是在Python中,else语句更像是作为一个模块,不仅仅可以和if语句搭配,还可以和循环语句,异常处理语句搭配使用.下面逐个进行介绍: ...

  4. 求奇数的乘积 AC 杭电

    求奇数的乘积 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  5. C++是怎么实现多态性的

    C++是怎么实现多态性的,C++中多态实现的原理, 当一个类中有虚函数时,系统会为该类构造一个虚函数表vtable,他是一个指针数组,存放每个虚函数的入口地址,编译器还会在此类中隐含插入一个指针vpt ...

  6. 有关mysql数据库的编码

    今天在通过表单给php提交数据,然后插入到数据库中.网页与php的编码格式均为utf-8,在插入到数据库中时也设置了$this->query("set names utf8;" ...

  7. Swift—do-try-catch错误处理模式-备

    Swift 1.x的错误处理模式存在很多弊端,例如:为了在编程时候省事,给error参数传递一个nil,或者方法调用完成后不去判断error是否为nil,不进行错误处理. let contents = ...

  8. [翻译]localStorage性能的好坏

    原文地址:Is localStorage performance a problem? 如果说2012年对于web开发世界来说有什么值得记住的事的话,关于localStorage性能的争论一定高居榜首 ...

  9. PHP 中的超全局变量

    (1)$_GET[].一个包含所有PHP 从客户端浏览器接收的GET变量的数组. (2)$_POST[].一个包含所有PHP 从客户端浏览器接收的POST变量的数组. (3)$_COOKIE[].一个 ...

  10. ActionResult 的返回类型

    大多数操作方法会返回从 ActionResult 中派生的类的实例. ActionResult 类是所有操作结果的基础. 不过,也存在不同的操作结果类型,具体取决于操作方法执行的任务. 例如,最常见的 ...