当前位置:网站首页>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:498. Diagonal traversal
- JVM quick start
- Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
- Generator parameters incoming parameters
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- [OC]-<UI入门>--常用控件-UIButton
- win10系统中的截图,win+prtSc保存位置
- 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
- Delay initialization and sealing classes
- Notes 01
猜你喜欢
【嵌入式】Cortex M4F DSP库
Charging interface docking tutorial of enterprise and micro service provider platform
Restful API design specification
【剑指offer】序列化二叉树
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
【嵌入式】使用JLINK RTT打印log
C语言双指针——经典题型
UnsupportedOperationException异常
[embedded] cortex m4f DSP Library
[embedded] print log using JLINK RTT
随机推荐
hutool优雅解析URL链接并获取参数
Roguelike game into crack the hardest hit areas, how to break the bureau?
Nacos 的安装与服务的注册
LeetCode:剑指 Offer 42. 连续子数组的最大和
同一局域网的手机和电脑相互访问,IIS设置
LeetCode:221. 最大正方形
Tcp/ip protocol
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Leetcode: Sword finger offer 48 The longest substring without repeated characters
Delay initialization and sealing classes
C語言雙指針——經典題型
LeetCode:236. 二叉树的最近公共祖先
Purpose of computer F1-F12
ROS compilation calls the third-party dynamic library (xxx.so)
To effectively improve the quality of software products, find a third-party software evaluation organization
深度剖析C语言指针
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
The network model established by torch is displayed by torch viz
Screenshot in win10 system, win+prtsc save location