当前位置:网站首页>【Hot100】34. Find the first and last positions of elements in a sorted array
【Hot100】34. Find the first and last positions of elements in a sorted array
2022-07-05 13:14:00 【Wang Liuliu's it daily】
34. Find the first and last positions of the elements in the sort array
Medium question
Give you an answer The decreasing An array of integers arranged in sequence nums, And a target value target.
Please find out the start position and end position of the given target value in the array .
If the target value does not exist in the array target, return [-1, -1].
You must design and implement a time complexity of O(log n) The algorithm to solve this problem .
Answer key :
Within a range , Find a number , Ask to find the start and end positions of this element , The numbers in this range are monotonically increasing , That is, it has monotonic property , So you can use dichotomy to do .
Two bisections , The first dichotomy finds the first t>=target The location of , The second dichotomy looks for the last t<=target The location of .
If the search is successful, two position subscripts will be returned , Otherwise return to [-1,-1].
- Template I : When we will interval [l, r] Divide into [l,mid] and [mid + 1, r] when , The update operation is r = mid perhaps l = mid + 1, Calculation mid There is no need to add 1, namely mid=(l+r)/2.
- Template II : When we will interval [l, r] Divide into [l,mid−1] and [mid,r] when , The update operation is r=mid−1 perhaps l=mid, In order to prevent the dead cycle , Calculation mid Need to add 1, namely mid=(l+r+1)/2.
Be careful :
When the left boundary is to be updated to l = mid
when , Order mid =(l + r + 1)/2
, Equivalent to rounding up , It won't be because r Take a special value r = l + 1
And fall into a dead circle .
class Solution {
public int[] searchRange(int[] nums, int target) {
if(nums.length == 0) {
return new int[]{
-1,-1};
}
int l = 0, r = nums.length - 1; // Dichotomous range
while( l < r) // Find the starting position of the element
{
int mid = (l + r )/2;
if(nums[mid] >= target) r = mid;
else l = mid + 1;
}
if( nums[r] != target) {
return new int[]{
-1,-1}; // To find the failure
}
int L = r;
l = 0;
r = nums.length - 1; // Dichotomous range
while( l < r) // Find the end position of the element
{
int mid = (l + r + 1)/2;
if(nums[mid] <= target ) l = mid;
else r = mid - 1;
}
return new int[]{
L,r};
}
}
class Solution {
public int[] searchRange(int[] nums, int target) {
if(nums.length == 0){
return new int[]{
-1,-1};
}
int l = 0, r = nums.length - 1; // Dichotomous range Left and right closed
// Find the starting position of the element
while( l < r){
int mid = (l + r )/2;
if(nums[mid] >= target){
r = mid;
} else{
l = mid + 1;
}
}
if( nums[r] != target){
return new int[]{
-1,-1}; // To find the failure
}
int L = r;
l = 0; r = nums.length - 1; // Dichotomous range
// Find the end position of the element
while( l < r)
{
int mid = (l + r + 1)/2; // Be careful
if(nums[mid] <= target ){
l = mid;
} else {
r = mid - 1;
}
}
return new int[]{
L,r};
}
}
边栏推荐
- Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
- APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
- 爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
- leetcode:221. 最大正方形【dp状态转移的精髓】
- Concurrent performance test of SAP Spartacus with JMeter
- DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
- Introduction to sap ui5 dynamicpage control
- uni-app开发语音识别app,讲究的就是简单快速。
- Leetcode20. Valid parentheses
- Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
猜你喜欢
解决uni-app配置页面、tabBar无效问题
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
逆波兰表达式
Word document injection (tracking word documents) incomplete
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
Natural language processing series (I) introduction overview
Android本地Sqlite数据库的备份和还原
946. Verify stack sequence
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
Pycharm installation third party library diagram
随机推荐
Natural language processing from Xiaobai to proficient (4): using machine learning to classify Chinese email content
事务的基本特性和隔离级别
Talk about my drawing skills in my writing career
How to choose note taking software? Comparison and evaluation of notion, flowus and WOLAI
Small case of function transfer parameters
同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
“百度杯”CTF比赛 九月场,Web:Upload
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
国际自动机工程师学会(SAE International)战略投资几何伙伴
Default parameters of function & multiple methods of function parameters
Alibaba cloud SLB load balancing product basic concept and purchase process
函数传递参数小案例
##无监控,不运维,以下是监控里常用的脚本监控
碎片化知识管理工具Memos
量价虽降,商业银行结构性存款为何受上市公司所偏爱?
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
Principle and configuration of RSTP protocol
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched