diff --git a/docs/0048-rotate-image.adoc b/docs/0048-rotate-image.adoc index 989a329ea7..b91f8cacdb 100644 --- a/docs/0048-rotate-image.adoc +++ b/docs/0048-rotate-image.adoc @@ -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* = @@ -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* = @@ -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. 旋转图像 - 官方题解^] diff --git a/docs/images/0048-00.jpg b/docs/images/0048-00.jpg new file mode 100644 index 0000000000..7a5ce3f5fb Binary files /dev/null and b/docs/images/0048-00.jpg differ diff --git a/docs/images/0048-01.jpg b/docs/images/0048-01.jpg new file mode 100644 index 0000000000..08600a2d58 Binary files /dev/null and b/docs/images/0048-01.jpg differ diff --git a/docs/images/0048-02.png b/docs/images/0048-02.png new file mode 100644 index 0000000000..724dda2554 Binary files /dev/null and b/docs/images/0048-02.png differ diff --git a/docs/images/0048-03.png b/docs/images/0048-03.png new file mode 100644 index 0000000000..1a5630f0c6 Binary files /dev/null and b/docs/images/0048-03.png differ diff --git a/docs/images/0048-04.png b/docs/images/0048-04.png new file mode 100644 index 0000000000..2dee1caffa Binary files /dev/null and b/docs/images/0048-04.png differ diff --git a/docs/images/0048-05.png b/docs/images/0048-05.png new file mode 100644 index 0000000000..6cd135caf1 Binary files /dev/null and b/docs/images/0048-05.png differ diff --git a/logbook/202406.adoc b/logbook/202406.adoc index adb157ea75..4c9e8a7ec2 100644 --- a/logbook/202406.adoc +++ b/logbook/202406.adoc @@ -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} 道题。 diff --git a/src/main/java/com/diguage/algo/leetcode/_0048_RotateImage.java b/src/main/java/com/diguage/algo/leetcode/_0048_RotateImage.java index a73e928c03..fe57102584 100644 --- a/src/main/java/com/diguage/algo/leetcode/_0048_RotateImage.java +++ b/src/main/java/com/diguage/algo/leetcode/_0048_RotateImage.java @@ -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) { diff --git a/src/main/java/com/diguage/algo/leetcode/_0048_RotateImage_2.java b/src/main/java/com/diguage/algo/leetcode/_0048_RotateImage_2.java new file mode 100644 index 0000000000..b279acd855 --- /dev/null +++ b/src/main/java/com/diguage/algo/leetcode/_0048_RotateImage_2.java @@ -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[] +}