当前位置:网站首页>剑指 Offer 04. 二维数组中的查找
剑指 Offer 04. 二维数组中的查找
2022-06-27 11:54:00 【zmm_mohua】
剑指 Offer 04. 二维数组中的查找
题目

代码
#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;
}
边栏推荐
- Interviewer: with the for loop, why do you need foreach?
- C/s architecture
- 解压 .log.gz 文件
- Go Web 编程入门:验证器
- How to modify a node_ Files in modules
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
- In 2021, the global enhanced oil production surfactant revenue was about USD 202.3 million, and it is expected to reach USD 297.1 million in 2028
- Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
- 数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
猜你喜欢

I.MX6ULL启动方式

Detailed explanation of interprocess communication
![Dynamic programming [III] (interval DP) stone merging](/img/a4/82c4d63b8df5d092a96b80dd497147.jpg)
Dynamic programming [III] (interval DP) stone merging

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area](/img/b7/2358e8cf1cdaeaba77e52d04cc74d4.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area

nifi从入门到实战(保姆级教程)——身份认证
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction](/img/d9/f6735906a130834c4b3e28de2b2617.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction

微服务拆分

MapReduce principle analysis (in-depth source code)

Unity shader learning (II) the first shader
随机推荐
i.mx6ull(单片机) c语言环境搭建
Peak store app imitation station development play mode explanation source code sharing
Unity shader learning (II) the first shader
How to adjust an integer that is entered in Excel but always displays decimals?
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
MapReduce原理剖析(深入源码)
R language uses the poisgof function of epidisplay package to test the goodness of fit of Poisson regression and whether there is overdispersion
master公式
[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction
巅峰小店APP仿站开发玩法模式讲解源码分享
解压 .log.gz 文件
Histrix工作原理
Minimum editing distance (linear DP writing method)
nifi从入门到实战(保姆级教程)——身份认证
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
alibaba jarslink
2022CISCN华中 Web
Daily leetcode force deduction (21~25)
AUTOCAD——三种修剪方式
私藏干货分享:关于企业架构中如何进行平台化