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 ...
随机推荐
- js②
操作符 ECMA-262描述了一组用于操作数据值的操作符,包括算术操作符(如加号和减号).位操作符.关系操作符和相等操作符. 一元操作符 递增和递减操作符(++ --) 一元加和减操作符 对非数值应用 ...
- 使用PHPExcel导出文件
使用PHPExcel导出文件步骤及解析: 新建一个excel表格:实例化PHPExcel类 创建sheet(内置表):createSheet()方法,创建新的sheet方法 setActiveShee ...
- 微信平台上遇到的bug
做微信平台遇到的bug,没有什么方法修改,至今只是避免出现,还未解决 1.header的position:fixed定位:如果整个页面的高度不足屏幕高度时,安卓部分手机header与title之间会有 ...
- css display:inline-block 出现空格解决方案
一: 给父元素添加 display: table;(只加这一句在谷歌, safari里可以) 子元素加: display: table-cell(火狐) 两句都加完美解决 二: .nav ul{fon ...
- sphinx,coreseek安装
sphinx是国外的一款搜索软件. coreseek是在sphinx的基础上,增加了中文分词功能,换句话说,就是支持了中文. Coreseek发布了3.2.14版本和4.1版本,其中的3.2.14版本 ...
- 获取局域网中指定IP或是主机名称的所有文件夹及其搜索文件
最近做个功能在局域网中所有指定文件,于是花了点精力完成了部分功能,先贴上 using System; using System.Collections.Generic; using System.Co ...
- Sweet Alert
http://www.dglives.com/demo/sweetalert-master/example/ Sweet Alert A beautiful replacement for Javas ...
- (spring-第20回【AOP基础篇】)Spring与事务
要想了解Spring的事务,首先要了解数据库事务的基本知识,数据库并发会产生很多问题,Spring使用ThreadLocal技术来处理这些问题,那么我们必须了解Java的ThreadLocal技术.下 ...
- 输入两个正整数m和n,求其最大公约数和最小公倍数
public static void main(String[] args){ Scanner sc = new Scanner (System.in); int a,b; System.out ...
- JSP自定义标签——调用数据库(通过id号搜索相关信息)
一.创建新表(假设在master数据库下新建) 二.连接数据库 开始-->控制面板-->管理工具-->数据源-->系统DSN-->添加-->SQL Server-- ...