题目在这里 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. 高性能Socket组件和RPC,让你像写本地代码一样开发网络应用和分布式程序

    最近想试试C#能不能写出高性能的分布式组件,于是写了一个双工RPC,也当练手,下面是单连接的测试结果,非常给力.机器配置:U:E1230-v2,内存:ddr3-8G 1.递归调用    过程:Clie ...

  2. Swift—Cocoa Touch设计模式-备

    目标(Target)与动作(Action)是iOS和OS X应用开发的中事件处理机制.   问题提出 如图所示是一个ButtonLabelSample案例设计原型图,其中包含一个标签和一个按钮,当点击 ...

  3. PIN的经验和技巧

    以前总是吧PIN码排序弄成:9876543210或0123456789, 有的路由PJ好久还没出密码,但还是PJ了3~4个,大多数是抓跑, 所以,还是建议大家先跑包,实在跑不出就来PIN, 一般人的设 ...

  4. Oracle_OCP课程实验学习

    Linux启动oracl.查看lsnrctl状态,然后启动监听start.sqlplus / as sysdba 启动数据库.conn sys/jxsrpv as sysdba .startup Ad ...

  5. ural 1723 Sandro's Book

    http://acm.timus.ru/problem.aspx?space=1&num=1723 #include <cstdio> #include <cstring&g ...

  6. Eric Pement的单行awk命令收集

    超实用的. awk1line_zh-CN.txt 可惜中文版的网址好像不能访问了.. HANDY ONE-LINE SCRIPTS FOR AWK 30 April 2008 Compiled by ...

  7. C51汇编语言完整源码

    单片机最小系统,两位LED数码管由串口输出接两个164驱动,Lout,Rout为左右声道输出,SET,  ALT0, ALT1为三个按键,也可自己在开始的I/O定义改成你想用的I/O口:12M晶振,若 ...

  8. VCRedist.exe静默安装方法(转)

    INNO setup 制作安装包  的时候,发布VC++运行时 [Run]Filename: {app}vcredist_x86.exe; Parameters: /q; WorkingDir: {t ...

  9. java中post和get请求

    示例代码: package com.shareboxes.util; import java.io.BufferedReader; import java.io.IOException; import ...

  10. python遗传算法实现数据拟合

    python据说功能强大,触角伸到各个领域,网上搜了一下其科学计算和工程计算能力也相当强,具备各种第三方包,除了性能软肋外,其他无可指摘,甚至可以同matlab等专业工具一较高下. 从网上找了一个使用 ...