当前位置:网站首页>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]
}
};
边栏推荐
- Notes 01
- 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
- View computer devices in LAN
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- 深度剖析C语言指针
- [embedded] print log using JLINK RTT
- 优秀的软件测试人员,都具备这些能力
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
猜你喜欢
The harm of game unpacking and the importance of resource encryption
TP-LINK enterprise router PPTP configuration
Indentation of tabs and spaces when writing programs for sublime text
Compétences en mémoire des graphiques UML
LeetCode:221. Largest Square
【ROS】usb_ Cam camera calibration
win10系统中的截图,win+prtSc保存位置
Export IEEE document format using latex
marathon-envs项目环境配置(强化学习模仿参考动作)
Nacos 的安装与服务的注册
随机推荐
角色动画(Character Animation)的现状与趋势
TDengine 社区问题双周精选 | 第三期
JVM quick start
有效提高软件产品质量,就找第三方软件测评机构
How to conduct interface test? What are the precautions? Nanny level interpretation
hutool优雅解析URL链接并获取参数
Delay initialization and sealing classes
Niuke winter vacation training 6 maze 2
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
POI add write excel file
【ROS】usb_ Cam camera calibration
Visual implementation and inspection of visdom
LeetCode:214. 最短回文串
win10系统中的截图,win+prtSc保存位置
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
C語言雙指針——經典題型
Mobile phones and computers on the same LAN access each other, IIS settings
LeetCode:394. String decoding
Export IEEE document format using latex