Skip to content

Commit

Permalink
二刷48
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Sep 19, 2024
1 parent 3ca83b3 commit 522301a
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 1 deletion.
41 changes: 41 additions & 0 deletions docs/0048-rotate-image.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ You have to rotate the image https://en.wikipedia.org/wiki/In-place_algorithm[*i

*Example 1:*

image::images/0048-00.jpg[{image_attr}]

[subs="verbatim,quotes,macros"]
----
Given *input matrix* =
Expand All @@ -32,6 +34,8 @@ rotate the input matrix *in-place* such that it becomes:

*Example 2:*

image::images/0048-01.jpg[{image_attr}]

[subs="verbatim,quotes,macros"]
----
Given *input matrix* =
Expand All @@ -51,10 +55,47 @@ rotate the input matrix *in-place* such that it becomes:
]
----

== 思路分析

将矩阵周围一圈,划分成四个区域。如下图所示:

image::images/0048-02.png[{image_attr}]

这样每个区域长度相等,使用循环做数字交换即可。另外,借助回溯思想,利用递归推进层次。

image:images/0048-03.png[{image_attr}]

image:images/0048-04.png[{image_attr}]

下面辅助矩阵的方案也不错:

image:images/0048-05.png[{image_attr}]

官方题解中的翻转矩阵的方案也不错!

[[src-0048]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_0048_RotateImage.java[tag=answer]
----
--
二刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_0048_RotateImage_2.java[tag=answer]
----
--
====

== 参考资料

. https://leetcode.cn/problems/rotate-image/solutions/1228078/48-xuan-zhuan-tu-xiang-fu-zhu-ju-zhen-yu-jobi/?envType=study-plan-v2&envId=selected-coding-interview[48. 旋转图像 - 辅助矩阵 / 原地修改,清晰图解^]
. https://leetcode.cn/problems/rotate-image/solutions/526980/xuan-zhuan-tu-xiang-by-leetcode-solution-vu3m/?envType=study-plan-v2&envId=selected-coding-interview[48. 旋转图像 - 官方题解^]
Binary file added docs/images/0048-00.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0048-01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0048-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0048-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0048-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0048-05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,11 @@
|{doc_base_url}/0151-reverse-words-in-a-string.adoc[题解]
|✅ 更优解是倒序遍历数组,用双指针记录单词下标。

|{counter:codes}
|{leetcode_base_url}/rotate-image/[48. Rotate Image^]
|{doc_base_url}/0048-rotate-image.adoc[题解]
|✅ 回溯,或翻转,或行转列

|===

截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@
* @since 2019-10-24 00:57:55
*/
public class _0048_RotateImage {
// tag::answer[]
// tag::answer[]
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Rotate Image.
*
* Memory Usage: 36.3 MB, less than 100.00% of Java online submissions for Rotate Image.
*
* @author D瓜哥 · https://www.diguage.com
* @since 2019-10-24 00:57:55
*/
public void rotate(int[][] matrix) {
if (Objects.isNull(matrix) || matrix.length == 0) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/diguage/algo/leetcode/_0048_RotateImage_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.diguage.algo.leetcode;

public class _0048_RotateImage_2 {
// tag::answer[]

/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-09-19 17:35:53
*/
public void rotate(int[][] matrix) {
backtrack(matrix, 0, 0,
matrix.length, matrix.length);
}

private void backtrack(int[][] matrix,
int row, int column,
int rLen, int cLen) {
if (rLen <= 1) {
return;
}
// 旋转
for (int i = 0; i < cLen - 1; i++) {
int tmp = matrix[row][column + i];
matrix[row][column + i] = matrix[row + rLen - 1 - i][column];
matrix[row + rLen - 1 - i][column] = matrix[row + rLen - 1][column + cLen - 1 - i];
matrix[row + rLen - 1][column + cLen - 1 - i] = matrix[row + i][column + cLen - 1];
matrix[row + i][column + cLen - 1] = tmp;
}
backtrack(matrix, row + 1, column + 1, rLen - 2, cLen - 2);
}
// end::answer[]
}

0 comments on commit 522301a

Please sign in to comment.