前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode 34. Search for a Range

leetcode 34. Search for a Range

作者头像
用户1749219
发布2018-05-16 11:23:24
5480
发布2018-05-16 11:23:24
举报

问题描述:

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4].

解释:

  给一串数组,在其中找出某一个数第一次出现和最后一次出现的位置,放在一个数组返回,没找到就返回[-1,-1];如给定数组[5, 7, 7, 8, 8, 10]和数字8,结果就是[3,4]

程序(用js写的):

代码语言:javascript
复制
 1 /**
 2  * @param {number[]} nums
 3  * @param {number} target
 4  * @return {number[]}
 5  */
 6 var searchRange = function(nums, target) {
 7     var res=[];
 8     for(var i=0;i<nums.length;++i){
 9         if(nums[i]==target){
10             res.push(i);
11         }
12     }
13     if(res.length==0){
14         return [-1,-1];
15     }else{
16         return [res[0],res[res.length-1]];
17     }
18     
19 };
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-02-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档