Given an array of integers nums 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].

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]
------------------------------------------------------------------------------------
这个题是在leetcode和lintcode 上都有的,只不过题目里面给的参数有些是不同的,需要注意的,不过代码是基本相同的。
用遍历数组的方式可能会TLE,所以用二分更好。
emmm,二分查找也可以用递归方式来写的。当用二分查找方式得到一个不为-1数组下标时,向两边扩展,最终得到一个范围。
C++代码:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int ans = search(nums,,nums.size()-,target);
if(ans == -) return {-,-}; //归结根底,vector就是一个数组,可以用{}。
int left = ans,right = ans;
while(left > && nums[left-] == nums[ans]) left--;
while(right < nums.size() - && nums[right+] == nums[ans]) right++;
return {left,right};
}
int search(vector<int>& nums,int left,int right,int target){
if(left > right) return -;
int mid = left + (right - left)/;
if(nums[mid] == target) return mid;
if(nums[mid] > target) return search(nums,,mid-,target);
else return search(nums,mid+,right,target);
}
};

还有一个解法:

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int ans = search(nums,,nums.size()-,target);
if(ans == -) return {-,-};
int left = ans,right = ans;
while(left > && nums[left-] == nums[ans]) left--;
while(right < nums.size() - && nums[right+] == nums[ans]) right++;
return {left,right};
}
int search(vector<int>& nums,int left,int right,int target){
// if(left > right) return -1;
// int mid = left + (right - left)/2;
// if(nums[mid] == target) return mid;
// if(nums[mid] > target) return search(nums,0,mid-1,target);
// else return search(nums,mid+1,right,target);
if(nums.size() == ) return -;
while(left + < right){
int mid = left + (right - left)/;
if(nums[mid] > target) right = mid;
else if(nums[mid] < target) left = mid;
else return mid;
}
if(nums[left] == target) return left;
if(nums[right] == target) return right;
return -;
}
};

emmm,也可以看官方题解:https://leetcode.com/articles/find-first-and-last-position-element-sorted-array/

(二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range的更多相关文章

  1. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  2. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  3. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  4. [leetcode]34.Find First and Last Position of Element in Sorted Array找区间

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  5. leetcode [34] Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  6. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  7. 34. Find First and Last Position of Element in Sorted Array + 二分

    题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...

  8. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  9. leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array

    思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...

随机推荐

  1. MySQL InnoDB 日志管理机制中的MTR和日志刷盘

    1.MTR(mini-transaction) 在MySQL的 InnoDB日志管理机制中,有一个很重要的概念就是MTR.MTR是InnoDB存储擎中一个很重要的用来保证物理写的完整性和持久性的机制. ...

  2. iead2018创建JavaWe工程

    菜单栏中 File-> Project,弹出如下界面,选择 Java并勾选 Web Application 填写 Project Name 配置 tomcat 点击右上角的绿色的小锤子,然后打开 ...

  3. Linux内存描述之内存节点node--Linux内存管理(二)

    1 内存节点node 1.1 为什么要用node来描述内存 这点前面是说的很明白了, NUMA结构下, 每个处理器CPU与一个本地内存直接相连, 而不同处理器之前则通过总线进行进一步的连接, 因此相对 ...

  4. 如何设置非管理员用户配置特定的IIS站点

    如何设置非管理员用     户配置特定的IIS站点 一.           添加IIS管理服务 二.           启动管理服务 勾选启用远程连接后.点右边的应用 三.           设 ...

  5. Jquery自动补全插件的使用

    1.引入css和js  <script src="js/jquery-ui.min.js"></script> <link href="cs ...

  6. (八)Index and Query a Document

    Let’s now put something into our customer index. We’ll index a simple customer document into the cus ...

  7. AI MobileNet

    MobileNet,是针对移动和嵌入式设备的一类高效模型,基于流线型(streamlined)架构,使用深度可分离卷积(depthwise separable convolution)来构建轻量级深度 ...

  8. Android测试(四):Instrumented 单元测试

    原文:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html Instrume ...

  9. WebApi的版本控制

      using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using Sy ...

  10. C#模板设计模式使用和学习心得

    模板设计模式: 模版方法模式由一个抽象类和一个(或一组)实现类通过继承结构组成,抽象类中的方法分为三种: 抽象方法:父类中只声明但不加以实现,而是定义好规范,然后由它的子类去实现. 模版方法:由抽象类 ...