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 ...
随机推荐
- java之ubuntu12.04 开发环境配制
配置java开发环境,即安装jdk: 1.配置环境变量 ,更改/etc/profile文件:sudo gedit /etc/profile; 在文件最后加上如下几行(其实跟windows下的配置原理一 ...
- VMware Workstation安装MAC OS X系统
http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html
- C# Lamada表达式
Lambda表达式 "Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量.它可以包含表达式和语句,并且可用于创建委托 ...
- vs2012 opencv 配置
一直没有学习C++,以为该语言太过old,所以要学习新的咚咚.一番学习归来,在进行OpenCV的时候,还是要用到这个C++.几次琢磨,终于能够配置好相关的开发环境和问题初步处理,有些内容得赶快记录下来 ...
- 错误:媒体集有 2 个媒体簇,但只提供了 1 个 sql2005 备份错误。
错误:媒体集有 2 个媒体簇,但只提供了 1 个 sql2005 2010-10-19 11:44:06| 分类: sql|举报|字号 订阅 ql2005备份的时候,选择备份路径是一方面,但 ...
- GoldenGate Studio 12.2.1.1发布
OGG studio是一款图形化OGG配置部署产品,其主要特性:1. 逻辑层面设计OGG,不需要了解OGG细节:2. 最值实践加快常用场景的配置:3. 使用拖拉映射,自动匹配源和目标对象:4. 一键部 ...
- Java中文件的随机读写
[例 10-12]模仿系统日志,将数据写入到文件尾部. //********** ep10_12.java ********** import java.io.*; class ep10_12{ pu ...
- Hadoop基本操作
命令基本格式: hadoop fs -cmd < args > 1.ls hadoop fs -ls / 列出hdfs文件系统根目录下的目录和文件 hadoop fs -ls -R / 列 ...
- NOIP 考前 高斯消元练习
POJ 1830 列出n个方程右边为最后的情况 每一行代表第几个灯的情况,每一行代表是否按第几个按钮写出方程即可. #include <cstdio> #include <cstri ...
- Unity自动场景保存脚本
新建一个名为AutoSave的编辑器脚本,并放于Assets/Editor下. using System; using UnityEditor; using UnityEditor.SceneMana ...