当前位置:网站首页>Leetcode 704. Binary search
Leetcode 704. Binary search
2022-06-27 16:58:00 【chenyson】
difficulty : Easy to
frequency :99
subject : 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.
Ideas : Just look in two
Be careful :
- while Inside is l<=r;
- And then on the left , On the left ==>r=mid-1
- If it's on the right , On the right side ==>l=mid+1
- int In the array length Attribute , and String Inside length Is the method
class Solution {
public int search(int[] nums, int target) {
int len=nums.length;
int l=0;
int r=len-1;
while(l<=r){
int mid=(r+l)/2;
if(target==nums[mid]){
return mid;
}
if(target<=nums[mid]){
r=mid-1;
}
else{
l=mid+1;
}
}
return -1;
}
}
边栏推荐
- QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
- Domain name binding dynamic IP best practices
- National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
- 国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
- C language course design
- Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
- Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end
- 锚文本大量丢失的问题
- 字节跳动埋点数据流建设与治理实践
- Yyds dry inventory brief chrome V8 engine garbage collection
猜你喜欢
List to table
树莓派初步使用
Raspberry pie preliminary use
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
Open source 23 things shardingsphere and database mesh have to say
Annual comprehensive analysis of China's audio market in 2022
d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?
Sliding window + monotone queue concept and example (p1886 Logu)
基于 Nebula Graph 构建百亿关系知识图谱实践
A large number of missing anchor text
随机推荐
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth
Extract field year / quarter effect based on date
Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration
Performance problems caused by redis cache invalidation and competitive loading
Deeply digitise, lead cloud nativity and serve more developers
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
3.4 fixed number of cycles II
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
How to modify / display GPIO status through ADB shell
#yyds干货盘点#简述chromeV8引擎垃圾回收
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
Oracle概念三
【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。
Raspberry pie preliminary use
What do fast fashion brands care more about?
C language course design
C système de gestion de la charge de travail des enseignants en langues
[multithreading] thread communication scheduling, waiting set wait(), notify()
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
Drawing for example study of flashcc