当前位置:网站首页>Leetcode:704 binary search
Leetcode:704 binary search
2022-07-28 12:45:00 【Memorises1999】
Brush slowly today leetcode 了 , Follow the online Daniel's question brushing route step by step , The specific route is : Array -> Linked list -> Hashtable -> character string -> Stack and queue -> Trees -> to flash back -> greedy -> Dynamic programming -> graph theory -> Advanced data structure
The first step is : Array
Relevant theoretical basis
1、 An array is a collection of the same type of data stored in continuous memory space .
2、 Array subscripts are all from 0 At the beginning , The address of array memory space is continuous
When we delete or add elements , It's hard to avoid moving the addresses of other elements
3、c++ The two-dimensional array is continuous in the address space , and JAVA It is not
Title 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
Their thinking :
1、 The premise of binary search :1 Ordered array ,2 There are no duplicate elements in the array
2、 Binary search method involves interval range 、 The boundary conditions . To be clear about the definition of interval , The boundary conditions are treated according to the definition of interval
3、 Use two interval methods to solve problems : Left and right closed , Left closed right away
Method 1 : Left and right closed , That is to say [left, right]
The main points of :
- while (left <= right) To use <= , because left == right It makes sense , So use <=
- if (nums[middle] > target) :right To assign as middle - 1, Because of the current nums[middle] It must not be target, Then the end subscript position of the left interval to be found next is middle - 1
Specific code implementation
// Version of a
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1; // Definition target In a left closed right closed interval ,[left, right]
while (left <= right) { // When left==right, Section [left, right] Is still valid , So use <=
int middle = left + ((right - left) / 2);// Prevent overflow Equate to (left + right)/2
if (nums[middle] > target) {
right = middle - 1; // target In the left range , therefore [left, middle - 1]
} else if (nums[middle] < target) {
left = middle + 1; // target In the right range , therefore [middle + 1, right]
} else { // nums[middle] == target
return middle; // Target value found in array , Return the subscript directly
}
}
// Target value not found
return -1;
}
};
Method 2 : Left close right open, that is [left, right)
The main points of :
- while (left < right), Use here < , because left == right In the interval [left, right) It doesn't make sense
- if (nums[middle] > target) right Updated to middle, Because the current nums[middle] It's not equal to target, Go to the left section and keep looking , The search interval is left closed and right open , therefore right Updated to middle, namely : The next query interval will not be compared nums[middle]
Specific code implementation
// Version 2
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size(); // Definition target In the interval between left closed and right open , namely :[left, right)
while (left < right) { // because left == right When , stay [left, right) It's invalid space , So use <
int middle = left + ((right - left) >> 1);
if (nums[middle] > target) {
right = middle; // target In the left range , stay [left, middle) in
} else if (nums[middle] < target) {
left = middle + 1; // target In the right range , stay [middle + 1, right) in
} else { // nums[middle] == target
return middle; // Target value found in array , Return the subscript directly
}
}
// Target value not found
return -1;
}
};summary :
1、 The most important thing about binary search is to define the interval you define , Deal with the boundary conditions according to the defined interval
边栏推荐
- Introduction to resttemplate
- 【萌新解题】爬楼梯
- 恋爱男女十禁
- JSP自定义标签之自定义分页标签02
- Sliding Window
- STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
- 试用copilot过程中问题解决
- Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held
- [nuxt 3] (XII) project directory structure 3
- XIII Actual combat - the role of common dependence
猜你喜欢

设计一个线程池

Why do enterprises need the ability of enterprise knowledge management?

连通块&&食物链——(并查集小结)

Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property

30 years of open source community | 2022 open atom global open source summit 30 years of special activities of open source community were successfully held

揭秘界面控件DevExpress WinForms为何弃用受关注的MaskBox属性

04 pyechars 地理图表(示例代码+效果图)

开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开

Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?

Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself
随机推荐
利用依赖包直接实现分页、SQL语句
LeetCode206 反转链表
New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
上位机和三菱FN2x通信实例
Open source office (ospo) unveils Secrets
1331. Array sequence number conversion: simple simulation question
揭秘界面控件DevExpress WinForms为何弃用受关注的MaskBox属性
stm32 回环结构接收串口数据并处理
Functions and pointers in 08 go language
力扣315计算右侧小于当前元素的个数
大模型哪家强?OpenBMB发布BMList给你答案!
How to build knowledge management system in enterprises and institutions
Using dependent packages to directly implement paging and SQL statements
单调栈Monotonic Stack
Communication example between upper computer and Mitsubishi fn2x
Come to tdengine Developer Conference and have an insight into the future trend of data technology development
C structure use
[half understood] zero value copy
[nuxt 3] (XII) project directory structure 3
Unity加载Glb模型