当前位置:网站首页>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;
}
边栏推荐
- Tidb 6.0: making Tso more efficient tidb Book rush
- Microservice splitting
- Wechat applet realizes five-star evaluation
- 秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
- Mit6.031 software construction7 reading notesdesigning specifications
- $15.8 billion! 2021 the world's top15 most profitable hedge fund giant
- Peak store app imitation station development play mode explanation source code sharing
- Usage of rxjs mergemap
- 如何修改 node_modules 里的文件
- Maximum path and problem (cherry picking problem)
猜你喜欢
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
MapReduce实战小案例(自定义排序、二次排序、分组、分区)
In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
巅峰小店APP仿站开发玩法模式讲解源码分享
1. Mx6ull startup mode
[on Nacos] get started quickly
Secyun won the "2022 AI analysis · it operation and maintenance vendor panorama report" as the representative vendor of intelligent operation and maintenance aiops Market
Shell script learning notes
alibaba jarslink
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
随机推荐
application.properties 的配置信息
Dynamic programming [4] (counting class DP) example: integer partition
c/s 架构
esp32s3 IPERF例程测试 esp32s3吞吐量测试
The R language uses the follow up The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The labels parameter adds label information t
Drive to APasS! Use Mingdao cloud to manage F1 events
Maximum path and problem (cherry picking problem)
关于枚举类的两种用法
Comment modifier Node Fichiers dans les modules
ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Operators are also important if you want to learn the C language well
Interview shock 60: what will cause MySQL index invalidation?
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
Topic38——56. Consolidation interval
微服务拆分
和动态规划的第一次相遇
Nifi from introduction to practice (nanny level tutorial) - identity authentication
Self taught ADT and OOP
57. The core principle of flutter - layout process
MapReduce实战小案例(自定义排序、二次排序、分组、分区)