[Leetcode][048] Rotate Image 略详细 (Java)
题目在这里 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)的更多相关文章
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
- 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). ...
- LeetCode 048 Rotate Image
题目要求:Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 deg ...
- [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 ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode】048. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
随机推荐
- CSS3+HTML5学习笔记(1)
box内阴影: -moz-box-shadow:inset 30px #; -webkit-box-shadow:inset 30px #; box-shadow:inset 30px #; 多重文字 ...
- Html中input标签的使用
1.取消按钮按下时的虚线框 在input里添加属性值 hideFocus 或者 HideFocus=true 2.只读文本框内容 在input里添加属性值 readonly 3.防止退后清空的TEXT ...
- sql对日期的处理,一个存储过程示例
IF v_docType = 3 THEN update T_PATIENT_INFO set USER_NAME =userName ,SEX = v_sex,BIRTHDAY = to_date( ...
- mysql 查询每个分组前N条记录
mysql 查询每个分组前N条记录 假设存在表movie, 有字段 id, part(地区), mcount(观看次数) 现查询每个地区观看次数最多的3部movie, 则表 ###id虽未存在gro ...
- 转:内核中的内存申请:kmalloc、vmalloc、kzalloc、kcalloc、get_free_pages
在内核模块中申请分配内存需要使用内核中的专用API:kmalloc.vmalloc.kzalloc.kcalloc.get_free_pages;当然,设备驱动程序也不例外;对于提供了MMU功能的处理 ...
- ural 1613 For Fans of Statistics
#include <cstdio> #include <cstring> #include <map> #include <vector> #inclu ...
- CSS之Position详解
CSS的很多其他属性大多容易理解,比如字体,文本,背景等.有些CSS书籍也会对这些简单的属性进行大张旗鼓的介绍,而偏偏忽略了对一些难缠的属性讲解,有避重就轻的嫌疑.CSS中主要难以理解的属性包括盒型结 ...
- css属性之transition
浏览器支持 Internet Explorer 10.Firefox.Opera 和 Chrome 支持 transition 属性. Safari 支持替代的 -webkit-transition ...
- Windows服务器Pyton辅助运维--03.安装Visual Studio 的 Python 开发插件 PTVS
PTVS (Python Tools for Visual Studio) http://pytools.codeplex.com/ 当前版本:2.1 RC PTVS (Python Tools fo ...
- IT项目外包有哪些注意事项
2015年两会上,李克强总理在政府工作报告中首次提出“互联网+”计划,各种互联网创业项目也如雨后春笋般破土而出.由于很多创业团队并没有自己的技术团队,所以软件外包成为很多创业项目普遍采取的一种方案.在 ...