Search for a Range
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].
题目简述:给定一个有序的整型数组,找出给定的目标值的start和end下标。
算法的时间复杂度必须是O(log n)
如目标值没有发现,返回[-1,-1].
如给定一个数组[5,7,7,8,8,10],给定目标值8,
返回[3,4]。
思路:
按照折半查找的方法查找到给定的目标值,得到相应的下标,在下标的两侧进行查找,找到相同的值.
int* searchRange(int* nums, int numsSize, int target, int* returnSize)
{
int *res=(int*)malloc(sizeof(int)*);
for(int i=;i<;i++)res[i]=-;
int low=;
int high=numsSize-;
int start=-,end=-;
if(low>high)return res;
*returnSize=;
while(low<=high)
{
int mid=(low+high)/;
if(nums[mid]>target)
{
high=mid-;
}
else if(nums[mid]<target)
{
low=mid+;
}
else{
start=mid;
end=mid;
while(start>low&&nums[start-]==nums[start])start--;
while(end<high&&nums[end+]==nums[end])end++;
res[]=start;
res[]=end;
return res;
}
}
return res;
}
Search for a Range的更多相关文章
- 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 ...
- 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 ...
- [OJ] Search for a Range
LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- 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::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- [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
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
随机推荐
- 去除 UINavigationController.navigationBar下方的横线
self.navigationController.navigationBar.clipsToBounds=YES;
- what we do and how we behave
It comes after a report last week revealed the "brutal" treatment of terror suspects by th ...
- 关于由CSS2.1所提出的的BFC的理解与样例
今天在这里谈谈css中BFC.“BFC”是Block Formatting Context的缩写,这个概念是由CSS2.1提出来的,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用.满 ...
- MySQL5.6下使用xtrabackup部分备份恢复到MySQL5.7
现有需求:需要备份MySQL5.6环境下的部分表到MySQL5.7环境下并进行恢复 通过xtrabackup 实现部分备份有三种方式: 参考链接:http://blog.csdn.net/zhu197 ...
- JAVA vo pojo javabean dto区别
JavaBean 是一种JAVA语言写成的可重用组件.为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器.JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性 ...
- PHP下使用强大的imagick轻松生成组合缩略图
project: blog target: use-imagick-to-composite-images-thumbnail.md date: 2016-02-19 status: publish ...
- cocos2d-x 常用UI
CCSprite* sprite = CCSprite::create("CloseNormal.png"); sprite->setPosition(ccp(50, 50) ...
- redis基础总结
Redis 数据类型: String Hash String类型: 一个key对应一个value,二进制安全的. set方法:设置对应值的value set name value get方法:获取对应 ...
- 一些初级Java错误,不定期增加
1. Error: Dangling meta character '*' near index 0 对字符串使用split()方法截取 * ? + / | 等字符的时候会报以下异常 Dangling ...
- Android Soap实例
// 指定命名空间 private static final String NAMESPACE = "http://WebXml.com.cn/"; // 给出接口地址 priva ...