当前位置:网站首页>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;
}
}
边栏推荐
- Bit. Store: long bear market, stable stacking products may become the main theme
- How to improve it electronic equipment performance management
- 米哈游起诉五矿信托,后者曾被曝产品暴雷
- EMQ 助力青岛研博建设智慧水务平台
- localDateTime类型的时间(2019-11-19T15:16:17) 用oracle的时间范围查询
- Nemo of pulseaudio (22)
- C語言教師工作量管理系統
- IDE Eval reset unlimited trial reset
- Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
- Mihayou sued Minmetals trust, which was exposed to product thunderstorms
猜你喜欢

d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing

鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
![[multithreading] thread communication scheduling, waiting set wait(), notify()](/img/4b/b9312fafd1ef1bf438f7aec7665276.png)
[multithreading] thread communication scheduling, waiting set wait(), notify()
![[the way of programmer training] - 3 Character count statistics](/img/0c/e506571a4b22edc7c02a346909eeab.jpg)
[the way of programmer training] - 3 Character count statistics

Autodesk NavisWorks 2022 software installation package download and installation tutorial

d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
![[pyGame games] this](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)

The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor

阿里云刘珅孜:云游戏带来的启发——端上创新

d3dx9_ 39.dll how to repair -d3dx9_ 39.dll missing file download
随机推荐
10分钟掌握mysql的安装步骤
Unity 阴影——阴影平坠(Shadow pancaking)
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
Qt5 signal and slot mechanism (basic introduction to signal and slot)
当发布/订阅模式遇上.NET
Julia constructs diagonal matrix
Solving Poisson equation by tensorflow
如何提升IT电子设备效能管理
Simulated process scheduling
07. Express routing
Realize simple three-D cube automatic rotation
事务的隔离级别详解
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
C language teacher workload management system
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
Cloud security daily 220216: root privilege escalation vulnerability found on IBM SaaS integration platform needs to be upgraded as soon as possible
EMQ 助力青岛研博建设智慧水务平台
When the publish / subscribe mode encounters NET