【一天一道LeetCode】#34. Search for a Range
一天一道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的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- 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 ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
随机推荐
- [OpenCV] GpuMat and Mat, compare cvtColor perforemence
Introduction I am going to measure the performence of my two GT650M and compare GPU with CPU version ...
- 【伯乐在线】FACEBOOK产品设计总监:设计APP时的14个必考题
最近看到Facebook产品设计总监Julie Zhuo拷问产品的14个问题,非常靠谱.其中有3个问题堪称致命拷问: 1.使用前:这款产品是如何吸引到你的注意力的?2.开始使用:使用这款产品是轻松愉悦 ...
- 破解Oracle ERP 密码
1. 写作目的 1 2. 利用Toad或其它pl/sql工具在Oracle ERP Database中建立Package,源码如下 1 (1). ...
- android 网络连接 HttpGet HttpPost方法
1.本文主要介绍利用HttpGet和HtppPost方法来获取网络json数据. 代码如下: public HttpData(String Url,HttpGetDataListener listen ...
- 【问题汇总】ScrollView嵌套ListView的问题
因产品的需求,需要在ScrollView中嵌套ListView来达到效果.众所周知,ScrollVIew和ListView都是可滑动的容器,嵌套使用一定会出现一些问题. [html] view pla ...
- (NO.00004)iOS实现打砖块游戏(八):游戏中小球与砖块的碰撞
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 现在回到GameScene.m中,我们所有的碰撞处理就放在该类中 ...
- 微信公众号Unauthorized API function
在进行微信公众号第三方开发的时候经常遇到这个问题,有两个原因: 1. 你的公众号没有这个api的功能(比如你是个人订阅号等). 2. 你的公众号有这个功能,但是你公众号没有进行认证. 具体可以查看微信 ...
- Linux 基于IPC机制实现进程间的共享内存处理
今天学习了相关于IPC(InterProcess Communication ,进程间通信)的相关知识.就做个笔记,一来让大家检查一下我的理解方面是不是有错误,二来也为了能让更多的博友们了解到相关的知 ...
- MySQL数据库安装与配置详解(图文)
接下来看一下如何安装mysql数据库. 由于有更详细的教程资源,因此参考别人的文章以整理.安装教程参考自博客园文章http://www.cnblogs.com/sshoub/p/4321640.htm ...
- iOS中 加强日志输出 开发技术总结
对于那些做后端开发的工程师来说,看LOG解Bug应该是理所当然的事,但我接触到的移动应用开发的工程师里面,很多人并没有这个意识,查Bug时总是一遍一遍的试图重现,试图调试,特别是对一些不太容易重现的B ...