[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 ...
随机推荐
- js 懒加载
需要的js <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> < ...
- AMH4.2 Ftp账号路径修改设置
AMH4.2的ftp控制有点不尽如人意,每个ftp账号只能对应一个站点:如果按照面板所提供的权限,有多少个站就得设置多少个ftp账号,这一操作就会非常麻烦,造成效率低下:不过AMH5.0已经能够通过面 ...
- Matlab与外部接口:MAT文件基础
MAT 文件MAT文件是MATLAB使用的一种特有的二进制数据文件.MAT文件可以包含一个或者多个MATLAB 变量.MATLAB通常采用MAT文件把工作空间的变量存储在磁盘里,在MAT文件中不仅保存 ...
- PHP无限级分类生成树实例代码
分享一例php无限级分类生成树的代码,学习下php无限级分类的实现方法,有需要的朋友参考下. 一段非常精简的PHP无限极分类生成树方法,巧在引用. 例子,php实现无限级分类. 代码示例: ...
- 转:内核中的内存申请:kmalloc、vmalloc、kzalloc、kcalloc、get_free_pages
在内核模块中申请分配内存需要使用内核中的专用API:kmalloc.vmalloc.kzalloc.kcalloc.get_free_pages;当然,设备驱动程序也不例外;对于提供了MMU功能的处理 ...
- Storm测试报告
http://blog.csdn.net/jmppok/article/details/17614431
- ExpandableList列表的简单应用
package com.test;//Download by http://ww.codefans.netimport java.util.ArrayList;import java.util.Has ...
- [LeetCode] 61. Rotate List 解题思路
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 行内人解读开发一个App需要多少钱?
对于很多互联网的创业者来说,评估前期的创业成本是很重要的.在这几年的创业大潮中,伴随着“互联网+”和“互联网思维”的普及,很多创业项目选择了开发app作为创业项目的载体.在我接触到的很多创业者,找Ap ...
- Neighbour table overflow --- arp表溢出
[root@jiangyi01.sqa.zmf /home/ahao.mah] #grep . /proc/sys/net/ipv4/neigh/default/gc_thresh* /proc/sy ...