题目在这里 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. mysql时间int日期转换

    select from_unixtime(1350437720);select unix_timestamp(now());插入用 unix_timestamp(date)查询用from_unixti ...

  2. 【创建型】Builder模式

    生成器模式的主要思想:将产品对象的创建与表现分离开,并且同样的创建过程可以有不同的产品表现. 直白一点可以理解为:待创建的对象是复杂的,一般情况下是需要经过多个步骤的创建后,最终才能将完整产品创建好, ...

  3. thrift的简单实现

    1.使用windows实现,首先在apache官网下载一个thrift的编译工具,在项目中建一个文件叫add.thrift的文件,内容如下: namespace java com.vipshop.sa ...

  4. d039: 点的位置

    内容: 已知一平面直角坐标系中正方形的左上(-2,2)和右下(2,-2)的顶点坐标,,当给一个点的坐标,判断点和正方形的关系,在正方形内(含边上)输出True ,否则输出 False 输入说明: 一行 ...

  5. noi 04:网线主管

    04:网线主管 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 仙境的居民们决定举办一场程序设计区域赛.裁判委员会完全由自愿组成,他们承诺要组织一次史上最公正 ...

  6. 『C # 开发』VS 2008 修改默认生成代码模版

    作为Coder,或许会因为每次写代码前要把版权信息Ctrl+C/V上去而蛋疼   ╮(╯▽╰)╭ 可作为Code Copyer,又何尝不蛋疼呢,怎么会容忍作业上署上别人的姓名,学号,XXX 还是要先S ...

  7. d3可视化实战00:d3的使用心得和学习资料汇总

    最近以来,我使用d3进行我的可视化工具的开发已经3个月了,同时也兼用其他一些图表类库,自我感觉稍微有点心得.之前我也写过相关文章,我涉及的数据可视化的实现技术和工具,但是那篇文章对于项目开发而言太浅了 ...

  8. Ubuntu12.04 下搭建Java开发环境

    1:下载 jdk-7u40-linux-i586.tar.gz. 2:解压安装. (1)创建jvm目录:sudo mkdir -p /usr/lib/jvm (2)sudo tar zxvf ./ j ...

  9. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  10. 【转】中断处理函数中不用disable_irq而用disable_irq_nosync原因

    原文网址:http://blog.csdn.net/skyflying2012/article/details/8265869 今天在写触摸屏驱动时在中断处理函数中使用disable_irq关中断发现 ...