当前位置:网站首页>二分查找(leetcode704.很简单必会的)
二分查找(leetcode704.很简单必会的)
2022-07-17 01:00:00 【only-qi】
问题描述:
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
示例:
示例 1:
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4
示例 2:
输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1
上代码,拿去即可运行:
package com.onlyqi.test03.erfen;
/**
* @author
* @date 2022/7/11 16:38
* @description
*/
public class Test01 {
public static void main(String[] args) {
Integer[] array={1,4,7,9,22,66};
System.out.println("============="+erFenTest(array,9));
}
public static Integer erFenTest(Integer[] array,Integer target){
Integer start=0;
Integer end=array.length-1;
while (start<=end){
Integer prov=(start+end)/2;
if(array[prov]==target){
return prov;
}
if(array[prov]>target){
end=prov-1;
}else {
start=prov+1;
}
}
return -1;
}
}
我要刷300道算法题,第102道
边栏推荐
- 深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之ADASYN采样法]
- D. Permutation Restoration(贪心/双指针/set)
- Win10 network connection shows no network but Internet access
- zsh: command not found: mysql
- Gdb+vscode for debugging 8 - use core to analyze dead cycles, deadlocks, and segment errors
- 04_ Service registration Eureka
- Use RZ, SZ commands to upload and download files through xshell7
- [MCU simulation] (IX) instruction system - add, ADDC, sub, Inc, Dec, Da of arithmetic operation instructions
- [MCU simulation] (XVIII) control transfer instructions - empty operation instructions
- Backup kubernetes backup etcd data
猜你喜欢
![2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )](/img/e4/ff7f1e19583f42377307de7291f870.png)
2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )

Browser cannot open tensorboard
![Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]](/img/f6/cbfe32991449975fe51f2e2c0c1d47.png)
Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]

Win10 onedrive failure reinstallation

zsh: command not found: mysql

Data source object management (third-party object resources) & load properties file

MySQL optimized index

Powertor500t reports an error 0x01806803

Visual analysis of ncnn param file and bin model

Rhce8 Learning Guide Chapter 1 installing rhel8.4
随机推荐
After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
Data source object management (third-party object resources) & load properties file
Polynomial interpolation fitting (III)
无线用的鉴权代码
My most productive easypypi once again has been updated! V1.4.0 release
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
[MCU simulation] (XVI) control transfer instructions - unconditional transfer instructions, conditional transfer instructions
ubuntu清除cuda缓存
The installation software prompts that the program input point adddlldirectory cannot be located in the dynamic link library kernel32 DLL (download address at the end of the text)
Tools and methods - Excel plug-in xltools
JPA初识(ORM思想、JPA的基本操作)
Note: light source selection and Application
Ncnn thread
Bisenetv1 face segmentation
Several methods of face detection
ES6 learning notes - brother Ma at station B
[NoSQL] redis high availability and persistence
zsh: command not found: mysql
[MCU simulation] (I) proteus8.9 installation tutorial
Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]