Skip to content

Commit a5bdcb9

Browse files
author
minibear2333
committed
[+]更新若干mid题解
1 parent f9bf6a7 commit a5bdcb9

File tree

3 files changed

+217
-9
lines changed

3 files changed

+217
-9
lines changed

LeetCode/all/59.螺旋矩阵-ii.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* @lc app=leetcode.cn id=59 lang=golang
3+
*
4+
* [59] 螺旋矩阵 II
5+
*
6+
* https://leetcode-cn.com/problems/spiral-matrix-ii/description/
7+
*
8+
* algorithms
9+
* Medium (80.14%)
10+
* Likes: 408
11+
* Dislikes: 0
12+
* Total Accepted: 97.6K
13+
* Total Submissions: 122K
14+
* Testcase Example: '3'
15+
*
16+
* 给你一个正整数 n ,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* 输入:n = 3
24+
* 输出:[[1,2,3],[8,9,4],[7,6,5]]
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* 输入:n = 1
31+
* 输出:[[1]]
32+
*
33+
*
34+
*
35+
*
36+
* 提示:
37+
*
38+
*
39+
* 1
40+
*
41+
*
42+
*/
43+
44+
// @lc code=start
45+
func generateMatrix(n int) [][]int {
46+
var res [][]int
47+
if n == 0{
48+
return res
49+
}
50+
res = make([][]int,n)
51+
for i:=0;i<n;i++{
52+
res[i] = make([]int,n)
53+
}
54+
t,l,r,b := 0,0,n-1,n-1
55+
num,end := 1,n*n
56+
for num<=end{
57+
for i:=l;i<=r;i++{
58+
res[t][i] = num
59+
num++
60+
}
61+
t++
62+
for i:=t;i<=b;i++{
63+
res[i][r] = num
64+
num++
65+
}
66+
r--
67+
for i:=r;i>=l;i--{
68+
res[b][i] = num
69+
num++
70+
}
71+
b--
72+
for i:=b;i>=t;i--{
73+
res[i][l] = num
74+
num++
75+
}
76+
l++
77+
}
78+
return res
79+
}
80+
// @lc code=end
81+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* @lc app=leetcode.cn id=74 lang=golang
3+
*
4+
* [74] 搜索二维矩阵
5+
*
6+
* https://leetcode-cn.com/problems/search-a-2d-matrix/description/
7+
*
8+
* algorithms
9+
* Medium (41.01%)
10+
* Likes: 426
11+
* Dislikes: 0
12+
* Total Accepted: 132.3K
13+
* Total Submissions: 297.3K
14+
* Testcase Example: '[[1,3,5,7],[10,11,16,20],[23,30,34,60]]\n3'
15+
*
16+
* 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
17+
*
18+
*
19+
* 每行中的整数从左到右按升序排列。
20+
* 每行的第一个整数大于前一行的最后一个整数。
21+
*
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
*
28+
* 输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
29+
* 输出:true
30+
*
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
36+
* 输出:false
37+
*
38+
*
39+
*
40+
*
41+
* 提示:
42+
*
43+
*
44+
* m == matrix.length
45+
* n == matrix[i].length
46+
* 1
47+
* -10^4
48+
*
49+
*
50+
*/
51+
52+
// @lc code=start
53+
func searchMatrix(matrix [][]int, target int) bool {
54+
n1 := len(matrix)
55+
if n1==0{
56+
return false
57+
}
58+
n2 := len(matrix[0])
59+
n := n1*n2
60+
l,r := 0,n-1
61+
for l<=r{
62+
mid := (r-l+1)/2 + l
63+
fmt.Println(mid,mid/n2,mid%n2)
64+
num := matrix[mid/n2][mid%n2]
65+
if num<target{
66+
l = mid+1
67+
}else if num >target{
68+
r = mid-1
69+
}else{
70+
return true
71+
}
72+
}
73+
return false
74+
}
75+
76+
//简单搜索
77+
// func searchMatrix(matrix [][]int, target int) bool {
78+
// n := len(matrix)
79+
// if n == 0{
80+
// return false
81+
// }
82+
// var raw,col int
83+
// raw = n-1
84+
// for raw>=0 && col < len(matrix[raw]){
85+
// num := matrix[raw][col]
86+
// if num == target{
87+
// return true
88+
// }
89+
// if num > target{
90+
// raw--
91+
// }
92+
// if num < target{
93+
// col++
94+
// }
95+
// }
96+
// return false
97+
// }
98+
// @lc code=end
99+

LeetCode/hot100/medium.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
点击下列题目标题可以跳转到LeetCode中文官网直接阅读题目,提交代码。
1313
点击下列代码链接,可以直接跳转到我的GitHub代码页面。每道题一般精选一种解法,我的GitHub中可能收录多种解法代码,请自行查看。
1414

15-
### 题解
16-
17-
#### 3.无重复字符的最长子串
15+
### 3.无重复字符的最长子串
1816

1917
题目:[在字符串中找一个子串,要求连续无重复字符且最长,只需要返回最大长度](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/description/)
2018

@@ -68,7 +66,7 @@
6866

6967
代码:[golang](../all/215.数组中的第k个最大元素.go)
7068

71-
#### 15.三数之和
69+
### 15.三数之和
7270

7371
题目:[数组里有没有三个数加起来为0,找出所有可能的情况](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/description/)
7472

@@ -214,15 +212,45 @@ if text1[i] != text2[j] then dp[i][j] = max(dp[i-1][j],dp[i][j-1])
214212

215213
代码:[golang](../all/1143.最长公共子序列.go)
216214

217-
###
215+
### 59.螺旋矩阵-ii
218216

219-
题目:[]()
217+
题目:[给一个数字n,输出n*n的正方形螺旋矩阵](https://leetcode-cn.com/problems/spiral-matrix-ii/description/)
220218

221-
题解:
219+
题解:生成一个 n×n 空矩阵 mat,随后模拟整个向内环绕的填入过程:
220+
* 定义当前左右上下边界 l,r,t,b,初始值 num = 1,迭代终止值 tar = n * n;
221+
* 当 num <= tar 时,始终按照 从左到右 从上到下 从右到左 从下到上 填入顺序循环,每次填入后:
222+
* 执行 num += 1:得到下一个需要填入的数字;
223+
* 更新边界:例如从左到右填完后,上边界 t += 1,相当于上边界向内缩 1。
224+
* 使用num <= tar而不是l < r || t < b作为迭代条件,是为了解决当n为奇数时,矩阵中心数字无法在迭代过程中被填充的问题。
222225

223-
注意:
226+
最终返回 mat 即可。
227+
228+
[题解来源](https://leetcode-cn.com/problems/spiral-matrix-ii/solution/spiral-matrix-ii-mo-ni-fa-she-ding-bian-jie-qing-x/)
229+
230+
代码:[golang](../all/59.螺旋矩阵-ii.go)
231+
232+
### 搜索二维矩阵
233+
234+
题目:[递增的二维矩阵,搜索值是否在内部](https://leetcode-cn.com/problems/search-a-2d-matrix/description/)
235+
236+
题解:方法1,简单搜索
237+
238+
* 以二维数组左下角为原点,建立直角坐标轴。
239+
* 若当前数字大于了查找数,查找往上移一位。
240+
* 若当前数字小于了查找数,查找往右移一位。
241+
242+
[题解来源](https://leetcode-cn.com/problems/search-a-2d-matrix/solution/zuo-biao-zhou-fa-er-wei-shu-zu-zhong-de-nxfc8/)
243+
244+
方法2,二分搜索(独创)
245+
246+
既然列递增,且行递增,下一行所有数字都比上一行大,可以把二维数组拼接起来,视为递增的一纬度数组来处理
247+
248+
* 取行列长度n1 n2,得到全局数字个数n1*n2
249+
* 使用二分法,取数字时,mid/n2为行,mid%n2为列即可
250+
251+
[我的题解](https://leetcode-cn.com/problems/search-a-2d-matrix/solution/tui-hua-fa-yi-ci-er-fen-zhao-dao-shu-zi-k36is/)
224252

225-
代码:[golang](../all/)
253+
代码:[golang](../all/74.搜索二维矩阵.go)
226254

227255
### 最后
228256

0 commit comments

Comments
 (0)