当前位置:网站首页>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;
}
边栏推荐
- Utilisation de la file d'attente des messages
- Interview shock 60: what will cause MySQL index invalidation?
- Object serialization
- 消息隊列的使用
- MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
- 消息队列的使用
- Safe landing practice of software supply chain under salesforce containerized ISV scenario
- 浅谈珂朵莉树
- uni-app开发微信小程序动态渲染页面,动态改变页面组件模块顺序
- Mybaitis generator details
猜你喜欢

动态规划【四】(计数类dp)例题:整数划分

How to adjust an integer that is entered in Excel but always displays decimals?

esp32s3 IPERF例程测试 esp32s3吞吐量测试

JMETER连接DM8

Private dry goods sharing: how to implement platform in Enterprise Architecture

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

MapReduce实战小案例(自定义排序、二次排序、分组、分区)

pull request

Topic37——64. 最小路径和

号称史上最难618,淘宝数据盘点你做对了吗?
随机推荐
mybaitis生成器详解
How to participate in openharmony code contribution
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
What is the TCP 3-time handshake process?
MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
Topic38——56. Consolidation interval
Thinkphp6 interface limits user access frequency
C/s architecture
Neo4j:入门基础(一)之安装与使用
C # WPF realizes undo redo function
解压 .log.gz 文件
Dynamic programming [III] (interval DP) stone merging
Self taught ADT and OOP
Take stock of some easy-to-use and niche markdown editors
How to modify a node_ Files in modules
namespace ‘rlang’ 0.2.0 is being loaded, but &gt;= 0.3.0 is required
uni-app 使用escook/request-miniprogram插件发请求说明
R language dplyr package arrange function sorts dataframe data, sorts dataframe data through multiple data columns, specifies the first field to be sorted in descending order, and does not specify the
最短编辑距离(线性dp写法)
Jianmu continuous integration platform v2.5.0 release