Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2266b6e

Browse files
author
杨世超
committed
Create 0074. 搜索二维矩阵.md
1 parent 4da23c5 commit 2266b6e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

‎Solutions/0074. 搜索二维矩阵.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
## [0074. 搜索二维矩阵](https://leetcode.cn/problems/search-a-2d-matrix/)
2+
3+
- 标签:数组、二分查找、矩阵
4+
- 难度:中等
5+
6+
## 题目大意
7+
8+
**描述**:给定一个 `m * n` 大小的有序二维矩阵 `matrix`。矩阵中每行元素从左到右升序排列,每列元素从上到下升序排列。再给定一个目标值 `target`
9+
10+
**要求**:判断矩阵中是否存在目标值 `target`
11+
12+
**说明**:
13+
14+
- $m == matrix.length$。
15+
- $n == matrix[i].length$。
16+
- 1ドル \le m, n \le 100$。
17+
- $-10^4 \le matrix[i][j], target \le 10^4$。
18+
19+
**示例**:
20+
21+
```Python
22+
输入 matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
23+
输出 True
24+
```
25+
26+
## 解题思路
27+
28+
### 思路 1:二分查找
29+
30+
二维矩阵是有序的,可以考虑使用二分搜索来进行查找。
31+
32+
1. 首先二分查找遍历对角线元素,假设对角线元素的坐标为 `(row, col)`。把数组元素按对角线分为右上角部分和左下角部分。
33+
2. 然后对于当前对角线元素右侧第 `row` 行、对角线元素下侧第 `col` 列进行二分查找。
34+
1. 如果找到目标,直接返回 `True`
35+
2. 如果找不到目标,则缩小范围,继续查找。
36+
3. 直到所有对角线元素都遍历完,依旧没找到,则返回 `False`
37+
38+
### 思路 1:代码
39+
40+
```Python
41+
class Solution:
42+
# 二分查找对角线元素
43+
def diagonalBinarySearch(self, matrix, diagonal, target):
44+
left = 0
45+
right = diagonal
46+
while left < right:
47+
mid = left + (right - left) // 2
48+
if matrix[mid][mid] < target:
49+
left = mid + 1
50+
else:
51+
right = mid
52+
return left
53+
54+
def rowBinarySearch(self, matrix, begin, cols, target):
55+
left = begin
56+
right = cols
57+
while left < right:
58+
mid = left + (right - left) // 2
59+
if matrix[begin][mid] < target:
60+
left = mid + 1
61+
elif matrix[begin][mid] > target:
62+
right = mid - 1
63+
else:
64+
left = mid
65+
break
66+
return begin <= left <= cols and matrix[begin][left] == target
67+
68+
def colBinarySearch(self, matrix, begin, rows, target):
69+
left = begin + 1
70+
right = rows
71+
while left < right:
72+
mid = left + (right - left) // 2
73+
if matrix[mid][begin] < target:
74+
left = mid + 1
75+
elif matrix[mid][begin] > target:
76+
right = mid - 1
77+
else:
78+
left = mid
79+
break
80+
return begin <= left <= rows and matrix[left][begin] == target
81+
82+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
83+
rows = len(matrix)
84+
if rows == 0:
85+
return False
86+
cols = len(matrix[0])
87+
if cols == 0:
88+
return False
89+
90+
min_val = min(rows, cols)
91+
index = self.diagonalBinarySearch(matrix, min_val - 1, target)
92+
if matrix[index][index] == target:
93+
return True
94+
for i in range(index + 1):
95+
row_search = self.rowBinarySearch(matrix, i, cols - 1, target)
96+
col_search = self.colBinarySearch(matrix, i, rows - 1, target)
97+
if row_search or col_search:
98+
return True
99+
return False
100+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /