当前位置:网站首页>Brush notes - find
Brush notes - find
2022-07-21 19:00:00 【[email protected]】
For the search topic , Generally speaking, we can start with the two methods of violent solution and binary search .
One 、 In an ascending array , Find a target value
Given a Elements in ascending order 、 An integer array without duplicate numbers nums And a target value target , Write a function search nums Medium target, If the target value has a return subscript ( Subscript from 0 Start ), Otherwise return to -1
Solution 1 : Violence solution
int search(vector<int>& nums, int target) {
// write code here
// Violence solution
for(int i=0;i<nums.size();i++)
{
if(nums[i] == target)
return i;
}
return -1;
}
Solution 2 :
int search(vector<int>& nums, int target) {
// write code here
int l= 0;
int r = nums.size()-1;
if(!nums.size())
{
return -1;
}
while(l<=r)
{
int mid = l + (r - l)/2;
if(nums[mid] < target)
{
l = mid +1;
}
else if(nums[mid] > target)
{
r = mid -1;
}
else
{
return mid;
}
}
return -1;
}
Two 、 Search in a two-dimensional array
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
// Violence solution
for(int i = 0;i< array.size();i++)
for(int j = 0;j< array[i].size();j++)
{
if(array[i][j] == target)
{
return true;
}
}
return false;
}
};
Solution 2 : Two points search
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
// Two points search
for(int i=0;i<array.size();i++){
int low=0;
int high=array[i].size()-1;
while(low<=high){
int mid=(low+high)/2;
if(target>array[i][mid])
low=mid+1;
else if(target<array[i][mid])
high=mid-1;
else
return true;
}
}
return false;
}
};
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int rows = array.size();
int cols = array.size();
int i = rows-1; // Element coordinates in the lower left corner
int j = 0;
if(!array.size())
return false;
if(!array[0].size())
return false;
while(i>=0 and array[i][j])
{
if(target < array[i][j])
{
i--; // Find smaller elements , Start looking for the first number up
}
else if(target>array[i][j])
j++; // The element you are looking for is larger , Look back
else
return true; // find
}
return false;
}
};
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/202/202207210235288068.html
边栏推荐
- 最新开源!基于LiDAR的位置识别网络OverlapTransformer,RAL/IROS 2022
- Streamlit 数据科学必备工具
- ESB結合UMC雲平臺開發說明
- Graylog distributed log component to improve the efficiency of log checking!
- [mathematical modeling] juvenile delinquency | stepwise regression analysis | residual analysis rcoplot
- Stm32f40x minimum system
- V853开发板硬件资料——RISC-V核E907用户手册
- resttemplate调用post\get
- ACM集训7月5号
- ESP 特权隔离机制介绍
猜你喜欢
循环结构--while循环和do-while循环
【数学建模】青少年犯罪问题 | 逐步回归分析法stepwise函数 | 残差分析rcoplot
Zhongang Mining: four trends of fluorite industry development
Flutter Icons内置图标库MaterialIcons大全
Part I - Fundamentals of C language_ 10. Document operation
安防视频监控平台EasyCVR数据库字段无法更新,如何优化?
A code implementation of non_max_suppression (NMS)
Usage of readshow database + National Library Reference Alliance
Hongmeng harmonios deveco studio reported an error unistall_ FAILED_ INTERNAL_ ERROR
Flyter icons built-in icon library materialicons Encyclopedia
随机推荐
乐鑫ESP-RTC 实时音视频通信方案
Practice of online problem feedback module (IX): realize image upload function (Part 2)
ClickHouse表引擎
机器学习-单变量线性回归
C language operators
dp---背包问题
读秀数据库的用法+全国图书馆参考咨询联盟
ACM training July 5
synchronized锁范围
API Test
Oracle export table data -dmp file
rk3128扬声器spk 和耳机hp声音大小调试
Custom paging label
A code implementation of non_max_suppression (NMS)
C语言运算符
C语言类型转换
Machine learning univariate linear regression
Leetcode:06zigzag transformation
教程篇(7.0) 03. FortiClient EMS配置和管理 * FortiClient EMS * Fortinet 网络安全专家 NSE 5
Security video monitoring platform easycvr database fields cannot be updated, how to optimize?