当前位置:网站首页>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]
}
};
边栏推荐
- [NVIDIA development board] FAQ (updated from time to time)
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- 随手记01
- Current situation and trend of character animation
- Purpose of computer F1-F12
- Deep anatomy of C language -- C language keywords
- PC easy to use essential software (used)
- @JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
- Simple use of promise in uniapp
- Philosophical enlightenment from single point to distributed
猜你喜欢

Problems in loading and saving pytorch trained models

Charging interface docking tutorial of enterprise and micro service provider platform

Esp8266-rtos IOT development
![[MySQL] multi table query](/img/eb/9d54df9a5c6aef44e35c7a63b286a6.jpg)
[MySQL] multi table query

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

Navicat premium create MySQL create stored procedure

vb.net 随窗口改变,缩放控件大小以及保持相对位置

Using C language to complete a simple calculator (function pointer array and callback function)

Swagger setting field required is mandatory

Crash problem of Chrome browser
随机推荐
角色动画(Character Animation)的现状与趋势
LeetCode:498. Diagonal traversal
CSP first week of question brushing
Niuke winter vacation training 6 maze 2
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Export IEEE document format using latex
[OC]-<UI入门>--常用控件-UIButton
Computer graduation design PHP Zhiduo online learning platform
C语言深度解剖——C语言关键字
opencv+dlib实现给蒙娜丽莎“配”眼镜
Compétences en mémoire des graphiques UML
可变长参数
What are the common processes of software stress testing? Professional software test reports issued by companies to share
LeetCode:836. Rectangle overlap
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
Variable length parameter
Super efficient! The secret of swagger Yapi
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
C language double pointer -- classic question type
LeetCode:剑指 Offer 04. 二维数组中的查找