当前位置:网站首页>704 binary search
704 binary search
2022-07-06 08:35:00 【So, dream】
704. 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.
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
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 .
Ideas
For in Ordered non repeating array Find the problem of target value in , You can consider binary search to achieve .
The title says ,nums All elements in are No repetition Of , And nums Press Ascending order , So you can consider using binary search .
The logic of binary search is relatively simple , But it is easy to make mistakes in the treatment of boundary conditions . for example , We write while(left < right)
and while(left <= right)
The corresponding processing method is completely different .
Let's take a simple example , Suppose there is only one element in the array 5, The target target
Also equal to 5. If you use while(left < right)
Writing , that right
Should be initialized to 1,left
Initialize to 0. And if you use while(left <= right)
How to write it ,left
and right
Should be initialized to 0. So the difference between the two ways of writing lies in Treatment of boundary conditions .
So according to the treatment of boundary conditions , There are two ways to write binary search .
The first way to write it
Suppose the array element is [1,2,3,4,5,6,7]
,target = 3
, We use while(left < right)
Writing to achieve binary sorting .
In this way ,left = 0,right = 7
, The operation process is shown in the figure
The key to the code is , When nums[mid] < nums[right]
when ,right = mid
. In circulation right
It's open range , This should also be an open interval , bring into correspondence with .
The corresponding code is as follows :(Kotlin)
class Solution {
fun search(nums: IntArray, target: Int): Int {
var left = 0
var right = nums.size // [left,right) On the right is the open section ,right Set to nums.size
while (left < right) {
val mid = (left + right) / 2
if (nums[mid] < target) left = mid + 1
else if (nums[mid] > target) right = mid // The core of the code , In circulation right It's open range , This should also be an open interval
else return mid
}
return -1 // Can't find target , return -1
}
}
The second way
Suppose the array element is [1,2,3,4,5,6,7]
,target = 3
, We use while(left <= right)
Writing to achieve binary sorting .
In this way ,`left = 0,right = 76, The operation process is shown in the figure
The key to the code is , When nums[mid] < nums[right]
when ,right = mid - 1
. In circulation right
It's a closed interval , This should also be a closed interval , bring into correspondence with .
The corresponding code is as follows :(Kotlin)
class Solution {
fun search(nums: IntArray, target: Int): Int {
var left = 0
var right = nums.size - 1 // [left,right] The right side is the closed section ,right Set to nums.size - 1
while (left <= right) {
val mid = (left + right) / 2
if (nums[mid] < target) left = mid + 1
else if (nums[mid] > target) right = mid - 1 // The core of the code , In circulation right It's a closed interval , This should also be a closed interval
else return mid
}
return -1 // Can't find target , return -1
}
}
边栏推荐
- The mysqlbinlog command uses
- Roguelike游戏成破解重灾区,如何破局?
- Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
- MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Configuring OSPF load sharing for Huawei devices
- 电脑F1-F12用途
- 深度剖析C语言数据在内存中的存储
- Modify the video name from the name mapping relationship in the table
猜你喜欢
软件卸载时遇到trying to use is on a network resource that is unavailable
【ROS】usb_cam相机标定
Problems in loading and saving pytorch trained models
同一局域网的手机和电脑相互访问,IIS设置
Trying to use is on a network resource that is unavailable
Fibonacci sequence
Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
2022.02.13 - NC004. Print number of loops
【MySQL】数据库的存储过程与存储函数通关教程(完整版)
Restful API design specification
随机推荐
Leetcode question brushing (5.31) string
2022.02.13 - NC002. sort
JVM 快速入门
同一局域网的手机和电脑相互访问,IIS设置
Summary of MySQL index failure scenarios
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
gcc动态库fPIC和fpic编译选项差异介绍
Modify the video name from the name mapping relationship in the table
【ROS】usb_cam相机标定
Image, CV2 read the conversion and size resize change of numpy array of pictures
Indentation of tabs and spaces when writing programs for sublime text
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
堆排序详解
Process of obtaining the electronic version of academic qualifications of xuexin.com
C語言雙指針——經典題型
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
游戏解包的危害及资源加密的重要性
Sublime text using ctrl+b to run another program without closing other runs
China's high purity aluminum target market status and investment forecast report (2022 Edition)
China vanadium battery Market Research and future prospects report (2022 Edition)