Skip to content

Commit d3ee13e

Browse files
committed
47
1 parent 0c608d9 commit d3ee13e

3 files changed

+77
-2
lines changed

SUMMARY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@
4747
* [44. Wildcard Matching](leetCode-44-Wildcard-Matching.md)
4848
* [45. Jump Game II](leetCode-45-Jump-Game-II.md)
4949
* [46. Permutations](leetCode-46-Permutations.md)
50-
* [47. Permutations II](leetCode-47-Permutations-II.md)
50+
* [47. Permutations II](leetCode-47-Permutations-II.md)
51+
* [48. Rotate Image](leetCode-48-Rotate-Image.md)

leetCode-4-Median-of-Two-Sorted-Arrays.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public double findMedianSortedArrays(int[] nums1, int[] nums2) {
230230

231231
那么,中位数就可以表示如下
232232

233-
(左半部分最大值 + 右半部分最大值 )/ 2 。
233+
(左半部分最大值 + 右半部分最小值 )/ 2 。
234234

235235
(max ( A [ i - 1 ] , B [ j - 1 ])+ min ( A [ i ] , B [ j ])) / 2
236236

leetCode-48-Rotate-Image.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 题目描述(中等难度)
2+
3+
![](https://windliang.oss-cn-beijing.aliyuncs.com/48.jpg)
4+
5+
将一个矩阵顺时针旋转 90 度,并且不使用额外的空间。大概属于找规律的题,没有什么一般的思路,观察就可以了。
6+
7+
# 解法一
8+
9+
可以先转置,然后把每列对称交换交换一下。
10+
11+
![](https://windliang.oss-cn-beijing.aliyuncs.com/48_2.jpg)
12+
13+
```java
14+
public void rotate(int[][] matrix) {
15+
//以对角线为轴交换
16+
for (int i = 0; i < matrix.length; i++) {
17+
for (int j = 0; j <=i; j++) {
18+
if (i == j) {
19+
continue;
20+
}
21+
int temp = matrix[i][j];
22+
matrix[i][j] = matrix[j][i];
23+
matrix[j][i] = temp;
24+
}
25+
}
26+
//交换列
27+
for (int i = 0, j = matrix.length - 1; i < matrix.length / 2; i++, j--) {
28+
for (int k = 0; k < matrix.length; k++) {
29+
int temp = matrix[k][i];
30+
matrix[k][i] = matrix[k][j];
31+
matrix[k][j] = temp;
32+
}
33+
}
34+
35+
}
36+
```
37+
38+
时间复杂度:O(n²)。
39+
40+
空间复杂度:O(1)。
41+
42+
也可以先以横向的中轴线为轴,对称的行进行交换,然后再以对角线交换。
43+
44+
# 解法二
45+
46+
我把这个[链接](https://leetcode.com/problems/rotate-image/discuss/18895/Clear-Java-solution)的思路贴过来,里边评论有张图也都顺道贴过来吧,写的很好。
47+
48+
![](https://windliang.oss-cn-beijing.aliyuncs.com/48_3.jpg)
49+
50+
一圈一圈的循环交换,很妙!
51+
52+
53+
54+
```java
55+
public void rotate(int[][] matrix) {
56+
int n=matrix.length;
57+
for (int i=0; i<n/2; i++)
58+
for (int j=i; j<n-i-1; j++) {
59+
int tmp=matrix[i][j];
60+
matrix[i][j]=matrix[n-j-1][i];
61+
matrix[n-j-1][i]=matrix[n-i-1][n-j-1];
62+
matrix[n-i-1][n-j-1]=matrix[j][n-i-1];
63+
matrix[j][n-i-1]=tmp;
64+
}
65+
}
66+
```
67+
68+
时间复杂度:O(n²)。
69+
70+
空间复杂度:O(1)。
71+
72+
#
73+
74+
这道题就是对题目的特征进行观察就可以了。

0 commit comments

Comments
 (0)