题目描述

给出一个有序数组,请在数组中找出目标值的起始位置和结束位置
你的算法的时间复杂度应该在O(log n)之内
如果数组中不存在目标,返回[-1, -1].
例如:
给出的数组是[5, 7, 7, 8, 8, 10],目标值是8,
返回[3, 4].

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

输入

复制

[5, 7, 7, 8, 8, 10],8

输出

复制

[3,4]


class Solution {
public:
    /**
     *
     * @param A int整型一维数组
     * @param n int A数组长度
     * @param target int整型
     * @return int整型vector
     */
    vector<int> searchRange(int* A, int n, int target) {
        // write code here
        vector< int> res(2,-1);
        if (A==nullptr || n<=0)
            return res;
        int low=lower_bound(A, A+n, target)-A;
        if (low==n || A[low]!=target)
            return res;
        else res[0]=low;
        int high=upper_bound(A, A+n,target)-A-1;
        res[1]=high;
        return res;
        
    }
};

class Solution {
public:
    /**
     *
     * @param A int整型一维数组
     * @param n int A数组长度
     * @param target int整型
     * @return int整型vector
     */
    vector<int> searchRange(int* A, int n, int target) {
        // write code here
        vector<int> res(2,-1);
        if (A==nullptr || n<=0)
            return res;
        int low=0,high=n-1;
        while (low<=high)
        {
            int middle =(high+low)>>1;
            if (A[middle]<target)
                low=middle+1;
            else
                high=middle-1;
            
        }
        int low2=0,high2=n-1;
        while (low2<=high2)
        {
            int middle2=(high2+low2)>>1;
            if (A[middle2]<=target)
                low2=middle2+1;
            else
                high2=middle2-1;
            
        }
        if (low<=high2){
            res[0]=low;
            res[1]=high2;
            
        }
        return res;
    }
    
};

leetcode116:search-for-a-range的更多相关文章

  1. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  2. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

  4. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

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

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

  6. 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 ...

  7. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

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

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

  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 ...

  10. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

随机推荐

  1. 1.入门篇十分钟了解Spring Cloud

    文章目录 Spring Cloud入门系列汇总 为什么需要学习Spring Cloud 什么是Spring Cloud 设计目标与优缺点 设计目标 优缺点 Spring Cloud发展前景 整体架构 ...

  2. 启动VNC Shell扩展

    下载source files - 18.3 Kb Introduction 我们使用RealVNC来远程控制我们的网络中的pc机,VNC是一个伟大的产品,但如果不记住计算机名称,它可以是乏味的,在网络 ...

  3. 部署MongoDB-4.2.7

    二进制部署很简单 创建存放软件目录下载解压 存放数据和日志及配置文件路径需要手工进行创建 mkdir -p /application/tools/ cd /application/tools/ wge ...

  4. PyCharm 上安装 Package(以 pandas 为例)

    一.使用 PyCharm 软件安装 pandas 包 1.打开 PyCharm 2.点击右上角 "Files" →"Settings..." 3.弹出" ...

  5. kubernetes-介绍与特性

    1. kubernetes概述 1) kubernetes是什么 2) kubernetes能做什么 3) kubernetes特性 4) kubernetes集群架构与组件 5) kubernete ...

  6. lua 源码阅读 1.1 -> 2.1

    lua 1.1 阅读1. hash.c 中 a) 对建立的 Hash *array 用 listhead 链式结构来管理,新增lua_hashcollector,用来做 Hash 的回收处理. ps: ...

  7. 正式班D8

    2020.10.15星期四 正式班D8 一.上节课复习 OSI七层协议 socket socket是对传输层以下的封装 IP+port标识唯一一个基于网络通讯的软件 TCP与UDP TCP:因为在通信 ...

  8. 99%的Android开发不得不面对的三道坎,到底该怎么破?

    今年比往年要特殊一些,受疫情的影响,很多公司都出现了裁员现象.以至于最近很多技术同学也在纷纷向我倒苦水. 王鹏便是其中的一员,王鹏之前是在一线城市的一家小型互联网公司做Android应用开发.从毕业实 ...

  9. Elasticsearch修改字段类型 (_reindex)

    1.设置索引t2为想要的数据类型 2.将t1 reindex到t2 3.数据reindex完成删除t1 4.设置索引t1为想要的数据类型 5.将t2 reindex到t1 如果 _reindex 超时 ...

  10. Jmeter设计压力测试场景

    1.哪些业务需要做压力测试? 比较常用的功能模块 单一业务场景或者多业务场景 项目需要做的业务场景 2.压力测试的并发数是多少? 有预期的数值?一次性达到?有上次性能测试的结果值? 无预期的数值?只有 ...