题目描述

给出一个有序数组,请在数组中找出目标值的起始位置和结束位置
你的算法的时间复杂度应该在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. 《C++primerplus》第7章练习题

    1.用户不断输入两个数,计算调和平均数,直到其中一个数为0. #include<iostream> using namespace std; double harm_mean(double ...

  2. JavaScript事件对象属性e.target和this的区别

    前言: Event对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 事件发生后,跟事件相关的一系列信息数据的集合都放到这个对象里面,这个对象就是事件对象eve ...

  3. ORA-00017: session requested to set trace event 请求会话以设置跟踪事件

    ORA-00017: session requested to set trace event   ORA-00017: 请求会话以设置跟踪事件 Cause:       The current se ...

  4. docker overlay原理

    周末两天研究了一下docker overlay网络的原理,因为我本身对go语言不太熟悉,直接看docker官方的libnetwork库看不太懂,看linux内核的vxlan代码又粗心大意,导致有一个环 ...

  5. JDBC的学习(一)

    JDBC的学习(一) 概念 所谓英文简写的意思是:Java DataBase Connectivity ,即 Java数据库的连接,用Java语言来操作数据库 本质 简单的来说,就是写这个JDBC的公 ...

  6. 【状态压缩DP】SCOI2009 围豆豆

    题目大意 洛谷链接 在一个\(N×M\)的矩阵方格内分布着\(D\)颗豆子,每颗豆有不同的分值\(V_i\).游戏者可以选择任意一个方格作为起始格,每次移动可以随意的走到相邻的四个格子,直到最终又回到 ...

  7. JVM系列【6】GC与调优2.md

    JVM系列笔记目录 虚拟机的基础概念 class文件结构 class文件加载过程 jvm内存模型 JVM常用指令 GC与调优 了解HotSpot常用命令行参数 JVM的命令行参数参考: https:/ ...

  8. SQL中 char varchar和nvarchar的区别

    转至:http://www.cnblogs.com/carekee/articles/2094676.html char    char是定长的,也就是当你输入的字符小于你指定的数目时,char(8) ...

  9. EFS加密

    目录 EFS简介 EFS的特点 EFS的缺陷 EFS证书 证书的导出 证书的安装 EFS加密 方法一 方法二 EFS简介 EFS(Encrypting File System,加密文件系统)是Wind ...

  10. 理解DES算法

    首先 了解对称密码加密技术:采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密.但是有时候密钥不用完全相同 只要相似也可以.因为用一个密钥可 ...