当前位置:网站首页>Binary search (leetcode704. very simple and necessary)
Binary search (leetcode704. very simple and necessary)
2022-07-19 03:29:00 【only-qi】
Problem 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.
Example :
Example 1:
Input : nums = [-1,0,3,5,9,12], target = 9
Output : 4
explain : 9 Appear in the nums And the subscript is 4
Example 2:
Input : nums = [-1,0,3,5,9,12], target = 2
Output : -1
explain : 2 non-existent nums So back to -1
Code up , Take it to run :
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;
}
}
I want to brush it 300 Algorithm questions , The first 102 Avenue
边栏推荐
- Display zabbix6.0 information using grafana8.5.2
- mysql复制表
- Backup kubernetes backup etcd data
- [MCU simulation] (XIX) introduction to assembly, assembly instructions, pseudo instructions
- In depth understanding of machine learning - unbalanced learning: sample sampling technology - [adasyn sampling method of manual sampling technology]
- Wechat applet
- Ubuntu clear CUDA cache
- Rtx3090 installing pytorch3d
- MySQL replication table
- Use RZ, SZ commands to upload and download files through xshell7
猜你喜欢

A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
![[MySQL] MHA high availability](/img/d3/d9830f3c331193fd40b8f00ebe35fa.png)
[MySQL] MHA high availability

374. 猜数字大小(入门 必会)

367. 有效的完全平方数(入门必会)

Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution

Vscode+ros2 environment configuration

MYSQL主从搭建
![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]

CorelDRAW cannot be installed. Solution

05_ Service call ribbon
随机推荐
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
367. 有效的完全平方数(入门必会)
First knowledge of JPA (ORM idea, basic operation of JPA)
leetcode 222. 完全二叉树的节点个数(必会)
oracle 关闭回收站
Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
[MCU simulation] (XV) instruction system bit operation instructions - bit operation instructions, bit conditional transfer instructions
[MCU simulation] (XXI) dB (define byte) - define byte
Basic IDL content of note 1: common data types_ Create array_ Type conversion_ Print output_ Basic operation_ Relational operation
Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)
JDBC connection to MySQL database
MySQL replication table
ES6學習筆記——B站小馬哥
无线用的鉴权代码
Obvious things
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
Gdb+vscode for debugging 8 - use core to analyze dead cycles, deadlocks, and segment errors
Unity解决同材质物体重叠产生Z-Fighting的问题
Labelme starts normally, but cannot be opened
MySQL面试题(2022)