当前位置:网站首页>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]
}
};
边栏推荐
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- Detailed explanation of dynamic planning
- Tcp/ip protocol
- Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
- The network model established by torch is displayed by torch viz
- Leetcode: Jianzhi offer 03 Duplicate numbers in array
- 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
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- C language double pointer -- classic question type
- 同一局域网的手机和电脑相互访问,IIS设置
猜你喜欢
随机推荐
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
To effectively improve the quality of software products, find a third-party software evaluation organization
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
Navicat premium create MySQL create stored procedure
Nacos 的安装与服务的注册
Revit 二次开发 HOF 方式调用transaction
Li Kou daily question 1 (2)
LeetCode:劍指 Offer 42. 連續子數組的最大和
UML diagram memory skills
swagger设置字段required必填
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
【嵌入式】使用JLINK RTT打印log
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
POI add write excel file
Pytorch view tensor memory size
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)
Hutool gracefully parses URL links and obtains parameters
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
[MySQL] multi table query
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串