题目描述

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

你的算法时间复杂度必须是 O(log n) 级别。

如果数组中不存在目标值,返回 [-1, -1]

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: [3,4]

示例 2:

输入: nums = [5,7,7,8,8,10], target = 6
输出: [-1,-1]

解题思路

利用二分查找的思想,分别找到数组中target出现的首位置和末位置,其中找首位置的步骤如下:

  • 若数组中点值小于target,继续在中点值之后的数组查找
  • 若数组中点值大于或等于target,说明前面数组中还可能有target,继续在中点值之前的数组查找
  • 最后当首位置大于末位置时,首位置即为数组中第一个大于或等于target的值
  • 若首位置上的数与target相等,则返回该位置,否则返回-1

同理可得到找末位置的步骤。

代码

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if(nums.empty())
return {-,-};
int first=findFirst(nums,target);
int last=findLast(nums,target);
return {first,last};
}
int findFirst(vector<int>& nums, int target){
int f=,l=nums.size()-;
while(f<=l){
int m=(f+l)/;
if(nums[m]<target)
f=m+;
else
l=m-;
}
if(f<nums.size()&&nums[f]==target)
return f;
return -;
}
int findLast(vector<int>& nums, int target){
int f=,l=nums.size()-;
while(f<=l){
int m=(f+l)/;
if(nums[m]<=target)
f=m+;
else
l=m-;
}
if(l>=&&nums[l]==target)
return l;
return -;
}
};

LeetCode 34. 搜索范围(search for a range)的更多相关文章

  1. 【LeetCode OJ 34】Search for a Range

    题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...

  2. LeetCode(34)Search for a Range

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

  3. LeetCode OJ:Search for a Range(区间查找)

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

  4. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  5. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  6. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  7. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  8. 【LeetCode】34. Search for a Range

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  9. leetcode 34 Search for a Range(二分法)

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

随机推荐

  1. python发起post请求获取json数据使用requests方法

    最普通的答案 我一直就觉得GET和POST没有什么除了语义之外的区别,自打我开始学习Web编程开始就是这么理解的 . 可能很多人都已经猜到了答案是: 1.GET 使用URL或Cookie传参.而POS ...

  2. 三剑客-awk(简写)

    特殊要点:$0 表示整个当前行$1 每行第一个字段NF 字段数量变量NR 每行的记录号,多文件记录递增OFS 输出字段分隔符, 默认也是空格,可以改为制表符等ORS 输出的记录分隔符,默认为换行符,即 ...

  3. JavaScript制作九九乘法表

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. mysoft

    @@a8649fbb56349908b5ca6708fb94b3ddabcf6b97381a9797d3dfb139b8749287117@@##74e02e1207e5a0a8996ba89f1d6 ...

  5. Delphi Edit组件

  6. 查看jar包依赖树

    在eclipse执行如下命令: 可以在控制台上查看层级依赖关系

  7. 去掉或修改lightinthebox网址与标题中Wholesale关键词

    includes\languages\english.php define('SEO_COMMON_KEYWORDS','Wholesale'); 将里面的Wholesale换成你想显示的词即可.

  8. DEC-UPDATE

    12/19-12/26 # -*- coding: utf-8 -*- import sys ans = [1,2,3,4,5,6] def operate(fun): a = ans[0] b = ...

  9. java将一数组乱序排列

    JAVA的Collections类中shuffle方法模拟了“洗牌”动作可以对list列表进行随机排序.如果一定要自己写,算法也很简单:假设数组array长度为n.用标准随机函数rand(n)生成[0 ...

  10. MongoDB的优势应用场景和配置

    一:MongoDB的简介: MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案 MongoDB是一个介于关系数据库和非关系数据库之间的 ...