一天一道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].

(二)解题

  1. /*
  2. 首先二分法查找目标值
  3. 然后沿着目标值左右延伸,依次找到相同值得左右边界
  4. */
  5. class Solution {
  6. public:
  7. vector<int> searchRange(vector<int>& nums, int target) {
  8. int i = 0;
  9. int j = nums.size()-1;
  10. int idx = -1;
  11. vector<int> ret;
  12. while(i <= j)
  13. {
  14. int mid = (i+j)/2;
  15. if(nums[mid] == target)
  16. {
  17. idx = mid;
  18. break;
  19. }
  20. else if(nums[mid]>target)
  21. {
  22. j = mid-1;
  23. }
  24. else if(nums[mid]<target)
  25. {
  26. i = mid+1;
  27. }
  28. }
  29. if(idx!=-1){
  30. int k = idx;
  31. while(k>=0 && nums[k] == target) k--;
  32. int m = idx;
  33. while(m<nums.size()&&nums[m] == target) m++;
  34. ret.push_back(k+1);
  35. ret.push_back(m-1);
  36. }
  37. else{
  38. ret.push_back(-1);
  39. ret.push_back(-1);
  40. }
  41. return ret;
  42. }
  43. };

【一天一道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. Bootstrap3 栅格系统-嵌套列

    为了使用内置的栅格系统将内容再次嵌套,可以通过添加一个新的 .row 元素和一系列 .col-sm-* 元素到已经存在的 .col-sm-* 元素内.被嵌套的行(row)所包含的列(column)的个 ...

  2. 高通开发笔记---Yangtze worknote

    点击打开链接 1. repo init -u git://review.sonyericsson.net/platform/manifest -b volatile-jb-mr1-yangtze 2. ...

  3. 记住经典的斐波拉契递归和阶乘递归转换为while规律

    记住经典的斐波拉契递归和阶乘递归转换为while规律.它为实现更复杂转换提供了启发性思路. # 斐波拉契--树形递归 def fab(n): if n<3: return n return fa ...

  4. YCSB性能测试工具使用

    在网上查In-Memory NoSQL性能测试的资料时,偶然间发现了这个性能测试工具YCSB,全称为"Yahoo! Cloud Serving Benchmark".它内置了对常见 ...

  5. iOS开发之UIWebView的常见一些用法

    虽然现在Xcode8已经开始使用WKWebView这个框架进行网页展示,但是UIWebView也有一些常用的方法需要知道,下面就简单展示一下,仅供大家参考 相关知识:1.设置背景透明:2.加载本地HT ...

  6. XMPP(二)-基于asmack+openfire的安卓客户端(仿QQ)的介绍以及个人心得

    关于XMPP第一篇-openfire的搭建写完后,就一直在赶本篇所要介绍的这个基于asmack+openfire的安卓客户端,费了不少精力,因为有不少同学在还在焦急的等待着(自恋了呵呵),所以紧赶慢赶 ...

  7. VIM编辑器操作命令积累

    开始学习VIM编辑器了,计划着每周学习几个新命令,记录在本篇博客中. 1.第一次接触 vi demo.txt 进入Normal模式查看文本 i 进入Insert模式插入内容,编辑文本 nG n代表行号 ...

  8. android 获取SD卡的图片及其路径

    1.首先是intent的设置: private static final int IMAGECODE = 0; Intent imageIntent = new Intent(Intent.ACYIO ...

  9. ThreadLocal的使用[代码片段]

    1.ThreadLocal定义,在一个类中定义: 在类A中: private static ThreadLocal<String> kcsHtmlPath = new ThreadLoca ...

  10. Servlet之Response对象

    下面的方法可用于在 Servlet 程序中设置 HTTP 响应报头.这些方法通过HttpServletResponse 对象可用. 1    String encodeRedirectURL(Stri ...