当前位置:网站首页>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:387. 字符串中的第一个唯一字符
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 自动化测试框架有什么作用?上海专业第三方软件测试公司安利
- 704 binary search
- How to conduct interface test? What are the precautions? Nanny level interpretation
- Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
- LeetCode:236. 二叉树的最近公共祖先
- LeetCode:394. String decoding
- LeetCode:498. 对角线遍历
猜你喜欢

同一局域网的手机和电脑相互访问,IIS设置

Alibaba cloud server mining virus solution (practiced)
![[OC]-<UI入门>--常用控件-UIButton](/img/4d/f5a62671068b26ef43f1101981c7bb.png)
[OC]-<UI入门>--常用控件-UIButton
![[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)](/img/af/a44c2845c254e4f48abde013344c2b.png)
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)

Roguelike game into crack the hardest hit areas, how to break the bureau?

Tcp/ip protocol

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures

Deep anatomy of C language -- C language keywords

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

Computer cleaning, deleted system files
随机推荐
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
Cesium draw points, lines, and faces
TP-LINK 企业路由器 PPTP 配置
数学建模2004B题(输电问题)
如何进行接口测试测?有哪些注意事项?保姆级解读
LeetCode:836. 矩形重叠
View computer devices in LAN
Swagger setting field required is mandatory
LeetCode:498. Diagonal traversal
TDengine 社区问题双周精选 | 第三期
LeetCode:236. The nearest common ancestor of binary tree
TCP/IP协议
To effectively improve the quality of software products, find a third-party software evaluation organization
Indentation of tabs and spaces when writing programs for sublime text
LeetCode:214. 最短回文串
vb. Net changes with the window, scales the size of the control and maintains its relative position
Alibaba cloud server mining virus solution (practiced)
Super efficient! The secret of swagger Yapi
[OC]-<UI入门>--常用控件的学习
Hutool gracefully parses URL links and obtains parameters