1、问题描述

Given an array of integers sorted in ascending order, 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].

2、问题分析

题目要求在log(N)的时间内。选择二分查找,现在数组中找出该数的一个位置,然后向左右两边扫描,找出左边的位置和右边的位置。

3、代码

 vector<int> searchRange(vector<int>& nums, int target) {
// 数组是有序的 ,先用二分查找找出一个目标数。然后向两边寻找 vector<int> ans; if(nums.size() == )
{
ans.push_back(-);
ans.push_back(-);
return ans;
} int left = ,right = nums.size()-;
int index = -;
while(left <= right)
{
int mid = left + (right - left)/;
if(nums[mid] == target)
{
index = mid;
break;
} else if( nums[mid] > target )
right = mid -;
else
left = mid + ;
} // target doesn't exist in array
if( index == -)
{
ans.push_back(-);
ans.push_back(-);
return ans;
} // target exist in array int indexL = index;
int indexR = index;
while( nums[indexL] == target && indexL >= ) indexL--;
while( nums[indexR] == target && indexR < nums.size() ) indexR++; int n = nums.size();
ans.push_back(indexL+);
ans.push_back(indexR-); return ans;

leetCode题解之寻找一个数在有序数组中的范围Search for a Range的更多相关文章

  1. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  2. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  3. [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素

    Given a sorted array consisting of only integers where every element appears twice except for one el ...

  4. leetCode题解之寻找插入位置

    1.问题描述 Search Insert Position Given a sorted array and a target value, return the index if the targe ...

  5. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  7. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  8. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  9. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

随机推荐

  1. ubuntu16.04搭建hadoop集群环境

    1. 系统环境Oracle VM VirtualBoxUbuntu 16.04Hadoop 2.7.4Java 1.8.0_111 master:192.168.19.128slave1:192.16 ...

  2. java数据结构之二叉树的定义和递归实现

    定义最多有两棵子树的有序树,称为二叉树.二叉树是一种特殊的树.递归定义:二叉树是n(n>=0)个有限结点构成的集合.N=0称为空二叉树:n>0的二叉树由一个根结点和两互不相交的,分别称为左 ...

  3. scala combineByKey用法说明

    语法是: combineByKey[C](   createCombiner: V => C,   mergeValue: (C, V) => C,   mergeCombiners: ( ...

  4. JavaScript位移运算多个大于号的使用方法

    JavaScript中的无符号位移运算符是用三个大于号来表示的 计算方法 例 100>>>2 100的二进制是 01100100 向右移2位后为 00011001 最后结果为25 1 ...

  5. printf()函数中\t,水平制表符,空格的个数

    在控制台输出数据的时候,也就是用printf()的时候,我们经常用\t来控制对齐,以使输出的结果更加整齐美观. 然而有时候我们发现及时使用了\t 也会出现数据对不齐的情况,这就跟\t究竟对应几个空格有 ...

  6. 【.Net】鼠标点击控制鼠标活动范围 ClipCursor

    可以使用API ClipCursor,如果你不嫌麻烦的话. 以下方法: Private Sub Form1_MouseDown(sender As System.Object, e As System ...

  7. [Hive]HiveSQL解析原理

    Hive是基于Hadoop的一个数据仓库系统,在各大公司都有广泛的应用.美团数据仓库也是基于Hive搭建,每天执行近万次的Hive ETL计算流程,负责每天数百GB的数据存储和分析.Hive的稳定性和 ...

  8. 糗事之 -- 用ssh公钥实现免密码登录

    前言:工作原因,每天都会登录好多次服务器,每次都是ssh root@192.168..... 然后输入密码,来来回回输几次真是很烦啊. 问题:怎么可以在每次ssh连接服务器时不用输入密码,直接登录? ...

  9. Linux信号机制代码示例

    1 基本功能: 本Blog创建了两个进程(父子进程): 父进程: 执行文本复制操作,当收到 SIGUSR1信号后,打印出现在文件复制的进度: 子进程: 每个固定时间段向父进程发送一个 SIGUSR1 ...

  10. webpack3+node+react+babel实现热加载(hmr)

    前端工程化开发的一个重要标志就是热替换技术,它大大的提高开发效率,使我们专注于写代码,webpack3中的热替换相比较1更加简洁. 1. 先看效果 Demo地址 https://github.com/ ...