题目描述

给出一个有序数组,请在数组中找出目标值的起始位置和结束位置
你的算法的时间复杂度应该在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. 【暂咕咕咕】SuffixTree

    #include<bits/stdc++.h> using namespace std; const int MAXN=1e6+10; typedef long long ll; char ...

  2. JavaScript按钮排他思想

    要求: 有一系列按钮,要求每单击其中一个,该按钮改变样式(以背景颜色为例),其他按钮恢复保持默认样式. 实现思路: 获取所有按钮元素 首先先把其他按钮的背景颜色去掉 再单独设置自己的样式 代码实现: ...

  3. 简说Modbus-RTU与Modbus-ASCII

    Modbus在串行总线通信中的协议有RTU和ASCII两种.RTU是Remote Terminal Unit的缩写,意思是远程终端单元.ASCII是American Standard Code for ...

  4. 【原创】xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 1.概述 上篇文章xenomai内核解析--实时IP ...

  5. 详解GaussDB(DWS) explain分布式执行计划

    摘要:本文主要介绍如何详细解读GaussDB(DWS)产生的分布式执行计划,从计划中发现性能调优点. 前言 执行计划(又称解释计划)是数据库执行SQL语句的具体步骤,例如通过索引还是全表扫描访问表中的 ...

  6. C# 采用Basic Auth传递Post或者GET 数据

    摘自:http://www.cnblogs.com/starcrm/p/4837971.html public class JiraApi{private string m_Username;priv ...

  7. 编写C语言的两种方法----Visual Studio/CodeBlocks

    1.CodeBlock(安装简单) 参考这个博客的:https://blog.csdn.net/jjjjkkjkk/article/details/80331625?utm_medium=distri ...

  8. python第二章:控制流

    变成实际上是一个过程的提现,每个过程都是有多个流程块组成. 比如:判断是否下雨的过程 1.布尔值 在第一章最后举例了比较操作后,最终返回的结果 True or False True 和 False是一 ...

  9. POI样式设置详细解析

    ````````由于看到网上对poi做报告合成的内容不是很全面, ````````自己最近又有新的需求, 作为勉励, 会慢慢补充详细的样式设置, 希望也能帮到各位 设置段落方向 (默认是纵向, 这里可 ...

  10. leetcode学习总结

    转自https://leetcode-cn.com/ 1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以 ...