题目意思:递增数组,找到目标数的范围,找不到则返回[-1,-1]

思路:折半查找

  1. class Solution {
  2. public:
  3. vector<int> searchRange(vector<int>& nums, int target) {
  4. int start=,end=nums.size()-;
  5. vector<int> ans;
  6. while(start<=end){
  7. int temp=(start+end)/;
  8. if(nums[temp]>target){
  9. end=temp-;
  10. }
  11. else if(nums[temp]<target){
  12. start=temp+;
  13. }
  14. else if(nums[temp]==target){
  15. int flag1=temp,flag2=temp;
  16. while(flag1>&&nums[flag1-]==target){
  17. --flag1;
  18. }
  19. while(flag2<nums.size()-&&nums[flag2+]==target){
  20. ++flag2;
  21. }
  22. ans.push_back(flag1);
  23. ans.push_back(flag2);
  24. return ans;
  25. }
  26. }
  27. ans.push_back(-);
  28. ans.push_back(-);
  29. return ans;
  30. }
  31. };

时间复杂度:O(logn)

运行时间:12ms

代码太冗余了,以后再精简吧

34 Search for a Range(目标数的范围Medium)的更多相关文章

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

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

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

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

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

  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

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  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. 34. Search for a Range

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

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

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. java实现版本号的比较

    之前比较客户端版本号,一直用的是String.compareTo,知道出现bug之后才明白了它的不完善地方.它的比较方式是按照字符串的比较来执行的,所以它有不正确的地方.举个例子,之前客户端版本号为: ...

  2. ArrayList 、Vector、 LinkList

    public class TestList {     public static void init(List list)     {         if(list!=null)          ...

  3. [Locked] Paint House I & II

    Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...

  4. oracle的nvl函数的使用解析

    Oracle的Nvl函数 nvl( ) 函数 从两个表达式返回一个非null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExpre ...

  5. Python进程、线程、协程详解

    进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源的管理和分配.任务的调度. ...

  6. ok6410 u-boot-2012.04.01移植六完善MLC NAND支持

    继ok6410 u-boot-2012.04.01移植四.五后,开发板基本已支持MLC NAND,支持DM9000.但是通过NAND命令更新u-boot到NAND,还存在问题,需要根据u-boot的n ...

  7. 微信支付bug

    1.最基本的操作就是检查各项参数正确2.确保将测试微信号加入测试白名单 3.目录正确:发起授权请求的页面必须是在授权目录下的页面,而不能是存在与子目录中.否则会返回错误,Android返回“Syste ...

  8. 从零开始学习jQuery-------jQuery元素选择器(三)

    下面我们来介绍一下jQuery元素选择器,在Web开发中我们最常用的操作是获取元素,然后对获取的元素进行一系列的操作,jQuery根据获取页面元素的不同,可以将jQuery选择器分为四大类:基本选择器 ...

  9. 刚入门的easyui

    这两天看了下easyui的教学先说说自己的一些小小理解吧! ----在使用easyui中也遇到了一个问题 : Uncaught TypeError:cannot call method ‘offset ...

  10. JS正则表达式验证账号、手机号、电话、邮箱、货币

    验证帐号是否合法验证规则:字母.数字.下划线组成,字母开头,4-16位. function checkUser(str){ var re = /^[a-zA-z]\w{3,15}$/; if(re.t ...