当前位置:网站首页>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]
}
};
边栏推荐
- LeetCode:394. String decoding
- LeetCode:剑指 Offer 42. 连续子数组的最大和
- LeetCode:498. 对角线遍历
- 力扣每日一题(二)
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- Nacos 的安装与服务的注册
- Detailed explanation of dynamic planning
- LeetCode:39. Combined sum
- marathon-envs项目环境配置(强化学习模仿参考动作)
- [OC]-<UI入门>--常用控件-UIButton
猜你喜欢

Cesium draw points, lines, and faces

Crash problem of Chrome browser

After reading the programmer's story, I can't help covering my chest...

Tcp/ip protocol

ROS compilation calls the third-party dynamic library (xxx.so)

PC easy to use essential software (used)

marathon-envs项目环境配置(强化学习模仿参考动作)

Deep anatomy of C language -- C language keywords

Charging interface docking tutorial of enterprise and micro service provider platform

Image, CV2 read the conversion and size resize change of numpy array of pictures
随机推荐
Tcp/ip protocol
项目连接数据库遇到的问题及解决
POI add write excel file
Hutool gracefully parses URL links and obtains parameters
Double pointeur en langage C - - modèle classique
View computer devices in LAN
To effectively improve the quality of software products, find a third-party software evaluation organization
Niuke winter vacation training 6 maze 2
Crash problem of Chrome browser
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)
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
[OC]-<UI入门>--常用控件-UIButton
C語言雙指針——經典題型
LeetCode:394. String decoding
Revit 二次开发 HOF 方式调用transaction
How to conduct interface test? What are the precautions? Nanny level interpretation
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
LeetCode:214. 最短回文串
Cesium draw points, lines, and faces