当前位置:网站首页>LeetCode->二分查找打卡
LeetCode->二分查找打卡
2022-07-26 23:17:00 【月半的人】
class Solution {
public int search(int[] nums, int target) {
if(nums.length==1){
return nums[0]==target?0:-1;
}
int left = 0;
int right = nums.length-1;
while(left<=right){
int middle = (left+right)/2;
if(nums[middle]<target){
left = middle+1;
}else if(nums[middle]>target){
right = middle-1;
}else{
return middle;
}
}
return -1;
}
}class Solution {
public int search(int[] nums, int target) {
if(nums.length==1){
return nums[0]==target?0:-1;
}
int left = 0;
int right = nums.length-1;
while(left<=right){
int middle = (left+right)/2;
if(nums[middle]<target){
left = middle+1;
}else if(nums[middle]>target){
right = middle-1;
}else{
return middle;
}
}
return -1;
}
}边栏推荐
猜你喜欢

Smooth data migration from single table to sub table

Record the nth SQL exception

I was fired at the age of 30. I want to understand a few things
![[draw sherpinski triangle in C language]](/img/e6/9d1d088d1c7675c23725443000329b.png)
[draw sherpinski triangle in C language]

How does the whole network display IP ownership?

见证中国网安力量 “解码2022中国网安强星”即将启航

Talk about the metrics of automated testing

NAT网络地址转换协议-拓扑实验

【你了解Cache吗——全面理解高速缓冲存储器】

How many holes have you stepped on in BigDecimal?
随机推荐
【用C语言绘制直角坐标系】
TCP的三次握手、四次断开
创业3年,现在鹅厂,年收入百万+,作为软件测试前辈的一些建议....
[C language programming] branch structure
NAT network address conversion experiment
砺夏行动|源启数字化:既有模式,还是开源创新?
C language -- while statement, dowhile statement, for loop and loop structure, break statement and continue statement
OSPF路由信息协议-拓扑实验
How does the whole network display IP ownership?
毕业进入HW,从测试工程师到项目经理,现如今在鹅厂年收入百万,我的给大家的一些建议...
[do you know cache - fully understand cache]
Multipoint bidirectional republication and routing strategy topology experiment
[enchanting interpretation, 15 minutes let you thoroughly learn how to use the stack!!!]
Open the door of programming
【洋哥带你玩转线性表(一)——顺序表】
软件测试基础理论知识—概念篇
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
证券公司哪家手续费最低?手机上开户安全吗
go语言慢速入门——go运算符
[brother Yang takes you to play with the linear table (III) - two way linked list]