Skip to content

Commit c5a6a24

Browse files
authored
Create searchmatrix.md
1 parent 7c462aa commit c5a6a24

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

2021/searchmatrix.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 搜索二维矩阵
2+
3+
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
4+
5+
每行的元素从左到右升序排列。
6+
每列的元素从上到下升序排列。
7+
8+
## 代码
9+
```
10+
class Solution {
11+
public:
12+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
13+
int col = 0, row = matrix.size() - 1;
14+
while(row >= 0 && col < matrix[0].size()){
15+
if(target > matrix[row][col]){
16+
col++;
17+
}else if(target < matrix[row][col]){
18+
row--;
19+
}else{
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
};
26+
```
27+
## 运行结果
28+
用时164ms,也太长了点吧。。。
29+
等下再用二分法试一下
30+
31+
## 考察方法
32+
### 分治算法
33+
把大的问题转换为小的问题解决
34+
### 二分算法
35+
分治算法的一种特例

0 commit comments

Comments
 (0)