当前位置:网站首页>Sword finger offer 04 Find in 2D array
Sword finger offer 04 Find in 2D array
2022-06-27 12:22:00 【zmm_ mohua】
The finger of the sword Offer 04. Search in a two-dimensional array
subject

Code
#include <iostream>
#include <vector>
using namespace std;
bool searchBinary(vector<int> nums, int target){
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + (right - left) / 2;
if(nums[mid] == target){
return true;
}else if(nums[mid] < target){
left = mid + 1;
}else if(nums[mid] > target){
right = mid - 1;
}
}
return false;
}
bool findNumberIn2DArray(vector<vector<int> >& matrix, int target) {
int n = matrix.size();
if(n == 0){
return false;
}
int m = matrix[0].size();
if(m == 0){
return false;
}
bool res;
for(int i = 0; i < n; i++){
if(matrix[i][0] > target){
return false;
}else if(matrix[i][0] <= target && target <= matrix[i][m-1]){
res = searchBinary(matrix[i], target);
if(res){
return true;
}
}
}
return false;
}
int main(){
int n, m, target;
bool res;
cin>>n>>m>>target;
vector<vector<int> > matrix(n, vector<int>(m));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cin>>matrix[i][j];
}
}
res = findNumberIn2DArray(matrix, target);
if(res){
cout<<"true";
}else{
cout<<"false";
}
return 0;
}
边栏推荐
- c/s 架构
- Topic37——64. Minimum path sum
- Nifi from introduction to practice (nanny level tutorial) - identity authentication
- Wechat applet realizes five-star evaluation
- MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
- 【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
- Dynamic programming [4] (counting class DP) example: integer partition
- 消息隊列的使用
- Microservice splitting
- 1. Mx6ull startup mode
猜你喜欢
随机推荐
Secyun won the "2022 AI analysis · it operation and maintenance vendor panorama report" as the representative vendor of intelligent operation and maintenance aiops Market
56. Core principle of flutter - flutter startup process and rendering pipeline
优博讯出席OpenHarmony技术日,全新打造下一代安全支付终端
面试突击60:什么情况会导致 MySQL 索引失效?
How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line
How to modify a node_ Files in modules
Nifi from introduction to practice (nanny level tutorial) - identity authentication
MapReduce实战小案例(自定义排序、二次排序、分组、分区)
如何修改 node_modules 裏的文件
What is the TCP 3-time handshake process?
Shell script learning notes
Jwas: a Bayesian based GWAS and GS software developed by Julia
master公式
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the AIC function is used to compare the AIC values of the two models (simple
What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?
居家办公被催之后才明白的时间管理
Object serialization
Tidb 6.0: making Tso more efficient tidb Book rush
MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
C # WPF realizes undo redo function



![Dynamic programming [4] (counting class DP) example: integer partition](/img/06/4b3863bbb85582348c6f4ea7c5511e.png)





