当前位置:网站首页>LeetCode:34. Find the first and last positions of elements in a sorted array
LeetCode:34. Find the first and last positions of elements in a sorted array
2022-07-06 08:51:00 【Bertil】
Given an array of integers in ascending order nums, And a target value target. Find the start 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].
Advanced :
- You can design and implement time complexity of O(log n) Does the algorithm solve this problem ?
Example 1:
Input :nums = [5,7,7,8,8,10], target = 8
Output :[3,4]
Example 2:
Input :nums = [5,7,7,8,8,10], target = 6
Output :[-1,-1]
Example 3:
Input :nums = [], target = 0
Output :[-1,-1]
Tips :
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- nums It is a group of non decreasing numbers
- -10^9 <= target <= 10^9
Their thinking
1. First, judge that the array is not empty and this element exists , And then use indexOf and lastIndexOf Method to find the first and last positions of the element
Code
/** * @param {number[]} nums * @param {number} target * @return {number[]} */
var searchRange = function(nums, target) {
if(nums !== [] && nums.indexOf(target) !== -1) {
let left = nums.indexOf(target)
let right = nums.lastIndexOf(target)
return [left, right]
}else {
return [-1, -1]
}
};
边栏推荐
- hutool优雅解析URL链接并获取参数
- Hutool gracefully parses URL links and obtains parameters
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- LeetCode:836. 矩形重叠
- UnsupportedOperationException异常
- Problems in loading and saving pytorch trained models
- LeetCode:498. 对角线遍历
- Navicat Premium 创建MySql 创建存储过程
- Screenshot in win10 system, win+prtsc save location
- After reading the programmer's story, I can't help covering my chest...
猜你喜欢

如何正确截取字符串(例:应用报错信息截取入库操作)

UML diagram memory skills

opencv+dlib实现给蒙娜丽莎“配”眼镜

Image, CV2 read the conversion and size resize change of numpy array of pictures
![[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born](/img/70/d275009134fcbf9ae984c0f278659e.jpg)
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born

Swagger setting field required is mandatory

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

Double pointeur en langage C - - modèle classique

LeetCode:236. The nearest common ancestor of binary tree

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
随机推荐
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
Niuke winter vacation training 6 maze 2
@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
LeetCode:221. 最大正方形
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
Nacos 的安装与服务的注册
深度剖析C语言数据在内存中的存储
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Hutool gracefully parses URL links and obtains parameters
TDengine 社区问题双周精选 | 第三期
704 binary search
To effectively improve the quality of software products, find a third-party software evaluation organization
[embedded] print log using JLINK RTT
Revit secondary development Hof method calls transaction
LeetCode:41. Missing first positive number
What are the common processes of software stress testing? Professional software test reports issued by companies to share
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
随手记01
Visual implementation and inspection of visdom