当前位置:网站首页>Array - 704. Binary search
Array - 704. Binary search
2022-07-23 22:15:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Two points search
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.
2 Title 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
3 Topic tips
- You can assume nums All elements in are not repeated .
- n Will be in [1, 10000] Between .
- nums Each element of will be in [-9999, 9999] Between .
4 Ideas
The premise of this topic is that the array is an ordered array , At the same time, the title also emphasizes that there are no duplicate elements in the array , Because once there's a repeating element , The element subscript returned by binary search method may not be unique , These are the prerequisites for using dichotomy , When you see that the Title Description meets the above conditions , But think about whether you can use dichotomy .
People often write dichotomy in disorder , Mainly because the definition of interval is not clear , The definition of interval is invariant . In the process of binary search , Keep invariants , Is in the while Every boundary treatment in the search must adhere to the operation according to the definition of interval , This is the loop invariant rule .
Write dichotomy , Interval is generally defined in two ways , Close left and close right [left, right], Or close left and open right [left, right).
5 My answer
( Version of a ) Left closed right closed interval
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid;
}
return -1;
}
}
( Version 2 ) Left closed right open interval
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid;
}
return -1;
}
}
边栏推荐
- "Morning reading" if you were in my position, what would you do? How do we do it,
- 【golang学习笔记】包(package)的使用
- experimental design
- Programmer growth Article 26: how to hold a good daily morning meeting?
- Use of [golang learning notes] package
- Programmation JDBC pour MySQL
- 王学岗视频编码————MediaCodec编解码
- 如何在 pyqt 中实现桌面歌词
- Memory search - DP
- [golang learning notes] is parameter transfer in go language value transfer or reference transfer
猜你喜欢

LeetCode高频题62. 不同路径:机器人从左上角到右下角的路径有多少条?纯概率排列组合问题,而不是动态规划题

Jedis 6 - Introduction and difference between redisson and jedis

ADB 命令结合 monkey 的简单使用,超详细

多线程问题:为什么不应该使用多线程读写同一个socket连接?

JS - event proxy and application scenarios

How does MySQL prepare SQL (solve the problem that in query SQL preprocessing can only query one record)

Golang invalid argument to intn报错的解决

Matlab小波工具箱导入信号出错(doesn‘t contain one dimensional Singal)

大淘营批量采集商品,如何将未上传的宝贝保存下来等后面再导入采集上传

Introduction to I2C Principle & Application of esp32
随机推荐
Can Verilog of synthetizable be integrated
【golang学习笔记】flag包的简单使用,命令行解析
University database creation and query practice -- database table design
Altium designer—Arduino UNO原理图&PCB图(自制Arduino板)
除了钱,创业者还需要什么?专访明月湖创赛创投机构
Record the process of the first excavation and intersection
Golang invalid argument to INTN
Tools in the tools package of Damon database (operate Damon database)
MySQL的JDBC编程
MySQL的 DDL和DML和DQL的基本语法
巴菲特股东大会的启示 2021-05-06
vip股票账户在手机开通安全吗?
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
How does MySQL prepare SQL (solve the problem that in query SQL preprocessing can only query one record)
lambda学习(sort后面的Comparator的使用,collection后使用Collectors.groupingBy分组)
Investment suggestions for overseas senior players (2) 2021-05-03
大学数据库创建与查询实战——数据库表设计
U++ 事件
STM32单片机使用ADC功能驱动手指检测心跳模块
Matlab小波工具箱导入信号出错(doesn‘t contain one dimensional Singal)