Skip to content

Commit f1cf26a

Browse files
authored
Update searchmatrix.md
1 parent c5a6a24 commit f1cf26a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2021/searchmatrix.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,30 @@ public:
3333
把大的问题转换为小的问题解决
3434
### 二分算法
3535
分治算法的一种特例
36+
## 待改结果
37+
想用二分法做的一道题,但是超时了,明天再改
38+
```
39+
class Solution {
40+
public:
41+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
42+
int high = matrix[0].size()-1, low= 0;
43+
int index = (high+low)/2;
44+
for (int i=0;i<matrix.size();){
45+
if(matrix[i][0]<target){
46+
i++;
47+
}
48+
else{
49+
index=(high+low)/2;
50+
if(matrix[i][index] < target){
51+
low=index+1;
52+
}
53+
else if (matrix[i][index] > target){
54+
high = index-1;
55+
}
56+
else return true;
57+
}
58+
}
59+
return false;
60+
}
61+
};
62+
```

0 commit comments

Comments
 (0)