File tree Expand file tree Collapse file tree
src/main/java/leetcode/_48_ Expand file tree Collapse file tree Load diff This file was deleted.
Original file line number Diff line number Diff line change 11# LeetCode-Java
2+
3+ 用 Java 刷刷 LeetCode, 保持思维活跃。
Original file line number Diff line number Diff line change 1+ package leetcode ._48_ ;
2+
3+ /**
4+ * Created by zhangbo54 on 2019-03-04.
5+ */
6+ public class Main {
7+ public static void main (String [] args ) {
8+ Solution solution = new Solution ();
9+
10+ int [][] arrays = {
11+ {1 , 2 , 3 , 4 },
12+ {5 , 6 , 7 , 8 },
13+ {9 , 10 , 11 , 12 },
14+ {13 , 14 , 15 , 16 }};
15+ solution .rotate (arrays );
16+ for (int i = 0 ; i < arrays .length ; i ++) {
17+ for (int j = 0 ; j < arrays .length ; j ++) {
18+ System .out .print (arrays [i ][j ] + "," );
19+ }
20+ System .out .println ("" );
21+ }
22+ System .out .println (arrays );
23+ }
24+ }
25+
Original file line number Diff line number Diff line change 1+ package leetcode ._48_ ;
2+
3+ class Solution { // 解决这个问题的关键就是知道,图像旋转90度是可以通过先对称镜像,然后对角线镜像可以得到旋转结果
4+ public void rotate (int [][] matrix ) {
5+ // 首先按照水平反转
6+
7+ for (int j = 0 ; j < matrix .length / 2 ; j ++) {
8+ for (int i = 0 ; i < matrix .length ; i ++) {
9+ int tmp = matrix [j ][i ];
10+ matrix [j ][i ] = matrix [matrix .length - 1 - j ][i ];
11+ matrix [matrix .length - 1 - j ][i ] = tmp ;
12+ }
13+ }
14+ // 然后按照对角线反转
15+ for (int i = 0 ; i < matrix .length ; i ++) {
16+ for (int j = i + 1 ; j < matrix .length ; j ++) {
17+ int tmp = matrix [j ][i ];
18+ matrix [j ][i ] = matrix [i ][j ];
19+ matrix [i ][j ] = tmp ;
20+ }
21+ }
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ ### [ 48\. Rotate ImageCopy for MarkdownCopy for Markdown] ( https://leetcode.com/problems/rotate-image/ )
2+
3+ Difficulty: ** Medium**
4+
5+
6+ You are given an _ n_ x _ n_ 2D matrix representing an image.
7+
8+ Rotate the image by 90 degrees (clockwise).
9+
10+ ** Note:**
11+
12+ You have to rotate the image , which means you have to modify the input 2D matrix directly. ** DO NOT** allocate another 2D matrix and do the rotation.
13+
14+ ** Example 1:**
15+
16+ ```
17+ Given input matrix =
18+ [
19+ [1,2,3],
20+ [4,5,6],
21+ [7,8,9]
22+ ],
23+
24+ rotate the input matrix in-place such that it becomes:
25+ [
26+ [7,4,1],
27+ [8,5,2],
28+ [9,6,3]
29+ ]
30+ ```
31+
32+ ** Example 2:**
33+
34+ ```
35+ Given input matrix =
36+ [
37+ [ 5, 1, 9,11],
38+ [ 2, 4, 8,10],
39+ [13, 3, 6, 7],
40+ [15,14,12,16]
41+ ],
42+
43+ rotate the input matrix in-place such that it becomes:
44+ [
45+ [15,13, 2, 5],
46+ [14, 3, 4, 1],
47+ [12, 6, 8, 9],
48+ [16, 7,10,11]
49+ ]
50+ ```
51+
52+
53+ #### Solution
54+
55+ Language: ** Java**
56+
57+ ``` java
58+ class Solution { // 解决这个问题的关键就是知道,图像旋转90度是可以通过先对称镜像,然后对角线镜像可以得到旋转结果
59+ public void rotate (int [][] matrix ) {
60+ // 首先按照水平反转
61+
62+ for (int j = 0 ; j < matrix. length / 2 ; j++ ) {
63+ for (int i = 0 ; i < matrix. length; i++ ) {
64+ int tmp = matrix[j][i];
65+ matrix[j][i] = matrix[matrix. length - 1 - j][i];
66+ matrix[matrix. length - 1 - j][i] = tmp;
67+ }
68+ }
69+ // 然后按照对角线反转
70+ for (int i = 0 ; i < matrix. length; i++ ) {
71+ for (int j = i + 1 ; j < matrix. length; j++ ) {
72+ int tmp = matrix[j][i];
73+ matrix[j][i] = matrix[i][j];
74+ matrix[i][j] = tmp;
75+ }
76+ }
77+ }
78+ }
79+ ```
80+ ![ ] ( http://ww2.sinaimg.cn/large/006tNc79ly1g4rmwqusm2j31bu0q6n1w.jpg )
81+ ![ ] ( http://ww2.sinaimg.cn/large/006tNc79ly1g4rmxspjwaj31410u0ard.jpg )
You can’t perform that action at this time.
0 commit comments