当前位置:网站首页>LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
2022-07-06 08:44:00 【Bertil】
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
进阶:
- 你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?
示例 1:
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
示例 2:
输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]
示例 3:
输入:nums = [], target = 0
输出:[-1,-1]
提示:
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- nums 是一个非递减数组
- -10^9 <= target <= 10^9
解题思路
1.首先判断数组非空且存在此元素,然后用indexOf和lastIndexOf方法来查找元素的第一个和最后一个位置
代码
/** * @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]
}
};
边栏推荐
- 深度剖析C语言数据在内存中的存储
- MySQL learning record 10getting started with JDBC
- China vanadium battery Market Research and future prospects report (2022 Edition)
- China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
- Trying to use is on a network resource that is unavailable
- C語言雙指針——經典題型
- 如何有效地进行自动化测试?
- [NVIDIA development board] FAQ (updated from time to time)
- sublime text中conda环境中plt.show无法弹出显示图片的问题
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
猜你喜欢
vb. Net changes with the window, scales the size of the control and maintains its relative position
Charging interface docking tutorial of enterprise and micro service provider platform
Swagger setting field required is mandatory
Visual implementation and inspection of visdom
MySQL learning records 12jdbc operation transactions
Detailed explanation of heap sorting
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
游戏解包的危害及资源加密的重要性
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
JS inheritance method
随机推荐
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
【ROS】usb_ Cam camera calibration
Mobile phones and computers on the same LAN access each other, IIS settings
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
hutool优雅解析URL链接并获取参数
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
On the inverse order problem of 01 knapsack problem in one-dimensional state
Deep anatomy of C language -- C language keywords
sys. argv
MySQL learning record 10getting started with JDBC
超高效!Swagger-Yapi的秘密
深度剖析C语言指针
Unified ordering background interface product description Chinese garbled
Generator parameters incoming parameters
可变长参数
查看局域网中电脑设备
How to conduct interface test? What are the precautions? Nanny level interpretation
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
Golang force buckle leetcode 1020 Number of enclaves
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)