一天一道LeetCode系列

(一)题目

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

(二)解题

/*
首先二分法查找目标值
然后沿着目标值左右延伸,依次找到相同值得左右边界
*/
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int i = 0;
        int j = nums.size()-1;
        int idx = -1;
        vector<int> ret;
        while(i <= j)
        {
            int mid = (i+j)/2;
            if(nums[mid] == target)
            {
                idx = mid;
                break;
            }
            else if(nums[mid]>target)
            {
                j = mid-1;
            }
            else if(nums[mid]<target)
            {
                i = mid+1;
            }
        }
        if(idx!=-1){
            int k = idx;
            while(k>=0 && nums[k] == target) k--;
            int m = idx;
            while(m<nums.size()&&nums[m] == target) m++;
            ret.push_back(k+1);
            ret.push_back(m-1);
        }
        else{
            ret.push_back(-1);
            ret.push_back(-1);
        }
        return ret;
    }
};

【一天一道LeetCode】#34. Search for a Range的更多相关文章

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

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

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

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

  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 34. Search for a Range (找到一个范围)

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

  6. leetcode@ [34] Search for a Range (STL Binary Search)

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

  7. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  8. [leetcode 34] search for a range

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

  9. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

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

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

随机推荐

  1. 关于前端HTML你需要知道的一切

    1.H系列标签(Header 1~Header 6) 作用: 用于给文本添加标题语义 格式: <h1>xxxxxx</h1> 注意点: H标签是用来给文本添加标题语义的, 而不 ...

  2. HBase的环境配置及其应用

    -------------------------------------------------------------------------------------- [版权申明:本文系作者原创 ...

  3. mysql字符集,insert,update,delete,select

    发现有错误:数据太长了.//查看数据库的所有编码:show variables like 'character%';-----+| character_set_client     | utf8    ...

  4. Thread 方法

    Thread类的一些被Thread对象调用的方法: 1 public void start() 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 2 public void run() ...

  5. String、StringBuffer、StringBuilder对比

    1.String 用于存放字符的数组被声明为final的,因此只能赋值一次,不可再更改.这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. Str ...

  6. Struts 2 之文件上传

    如果要获得上传文件的原始名称,需要定义一个String类型的属性,属性名必须为***FileName,其中***为File属性的名称:同理,如果要获取该文件的MIME类型,需要定义一个***Conte ...

  7. Spark技术内幕: Shuffle详解(二)

    本文主要关注ShuffledRDD的Shuffle Read是如何从其他的node上读取数据的. 上文讲到了获取如何获取的策略都在org.apache.spark.storage.BlockFetch ...

  8. 二维码扫描&集合排序

    一.二维码扫描机制 二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的:在代码编制上巧妙地利用构 ...

  9. 关于Lt分发系统的时序图分析

    我们已经知道,系统共分为两个模块,mather与son 同时系统允许的操作也有三种,向mather提交war包,我某个服务器更新代码,为所有服务器更新代码 我们一个一个来看 先说,向mather提交w ...

  10. Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)

    我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观. 今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求 ...