当前位置:网站首页>Leetcode 704 · binary search
Leetcode 704 · binary search
2022-04-21 07:46:00 【Konjaku in Grade 5 of primary school】
leetcode 704· Two points search
Hello everyone , I'm studying konjak in grade five of primary school , Focus on the back end , Witness the growth of konjaku together , Your comments, praise and attention are my biggest motivation , If you have any mistakes, please let me know , Thank you very much . Let's support originality ! There is a mistake in writing .
Title Description
Given a n The elements are ordered ( Ascending ) integer array nums And a target value target , Write a function search nums Medium target, If the target value has a return subscript , Otherwise return to -1.
Answer 1
Assuming that this array is a fully closed array, it is necessary to judge left==right The situation of
be left and right The value of should be +1
class Solution {
public:
int search(vector<int>& nums, int target) {
int left,right,mid;
left=0,right=nums.size()-1;
while (left <= right)
{
mid=(left+right)/2;
if(nums[mid] == target){
return mid;
}
else if(nums[mid] < target){
left=mid+1;
}
else {
right=mid-1;
}
}
return -1;
}
};
Explanation 2
Assuming that the interval range is a semi open and semi closed interval, then left and right There can be no equality , The cyclic condition can only be left<right
And the subscript movement of the open interval should not be reduced by one
class Solution {
public:
int search(vector<int>& nums, int target) {
int left,right,mid;
left=0,right=nums.size();
while (left < right)
{
mid=(left+right)/2;
if(nums[mid] == target){
return mid;
}
else if(nums[mid] < target){
left=mid+1;
}
else {
right=mid;
}
}
return -1;
}
};
版权声明
本文为[Konjaku in Grade 5 of primary school]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626569488.html
边栏推荐
猜你喜欢

Lecture de l'article: mesurer l'impact d'un accès DDOS réussi sur le comportement du client du serveur DNS géré

SMS logic vulnerability

Flutter初体验

关于数据治理平台中数据仓库ODS、DW和DM概念理解

404 problem in idea configuration servlet solution

Enterprise service bus -- Introduction to muleesb
![Navicat even Oracle reports an error [no matching authentication protocol]](/img/16/4dd115fc5abc68f7d1ffa640165fd9.png)
Navicat even Oracle reports an error [no matching authentication protocol]

服务器部署svn环境

When deploying. Net core on Linux platform to access SQL Server 2008 R2, it prompts the connection timeout exception

论文阅读:Social Media and Fake News in the 2016 Election
随机推荐
Steps for umlet beginners
论文阅读:Cached and Confused: Web Cache Deception in the wild
文件打包下载
php 二维数组转一维数组
php Rsa加密
服务器部署svn环境
使用Hutool的流方式读取Excel大文件
SMS logic vulnerability
西方的二分法和东方的三分法
Accumulation of DB2 related operation knowledge points and instance of DB2 connecting to remote database in Windows Environment
微信公众号查看uin,把它base64后即为biz
laravel实现打包多个文件,并下载
Defer
MYSQL中批量替换某个字段的部分数据
虚拟机 主机 ping ssh 校园网 桥接 NET
delphi的json类:SuperObject,以及简单用法jsonHelper
Navicat连Oracle报错[No matching authentication protocol]
Enterprise service bus -- Introduction to muleesb
标准函数返回值iResult
.net core 将错误抛出写入.txt文件