题目描述:

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].

结题思路:

采用二分法查找,如果找到再从这个位置向两边扩散,直到到达目标值的两边边界。

代码如下:

public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] result = { -1, -1 };
int left = 0;
int right = nums.length - 1;
int mid, low, high; if (target > nums[right] || target < nums[left])
return result; while (left <= right) {
mid = (left + right) / 2;
if (nums[mid] == target) {
low = mid;
high = mid;
while (low >= 0 && nums[low] == target)
low--;
result[0] = low + 1;
while (high < nums.length && nums[high] == target)
high++;
result[1] = high - 1;
return result;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return result;
}
}

Java [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. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

随机推荐

  1. PL/SQL数据导入导出浅谈(1)

    近来需要通过PL/SQL向Oracle中导数据,特此总结一下 试例表:test 字段:id;name;org; 1.直接复制粘贴(当数据量不是特别大的时候) 1)使用select * from tes ...

  2. poi导出到excel步骤分析

    在没用过poi之前感觉poi是很高大上的样子, 项目中用了发现poi的代码重复性很高类似于jdbc的模板代码, 项目中如果大量使用最好封装起来; 总结一下归结为6步 1 打开或新创建一个工作薄(使用H ...

  3. python学习笔记5(元组)

    一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这个组默认是元组类型3.所有的多对象,逗号分隔的,没有明确用符号定义的这些都默认为元组类型 >& ...

  4. Excel 隐藏功能区命令

    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"

  5. 团体程序设计天梯赛-练习集L1-023. 输出GPLT

    L1-023. 输出GPLT 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个长度不超过10000的.仅由英文字母构成的 ...

  6. Nagios+msn+fetion自定义时间发送报警消息

    转自http://blog.csdn.net/deccmtd/article/details/6063467 Nagios+fetion发送手机报警使用了几个月.每次报警短信来都要看下手机.感觉麻烦. ...

  7. 可以继承的C++ Singleton基类

    单例模式(Singleton Pattern)是设计模式中的一种,它用来保证系统中最多只能存在一个它的实例,其做法是由类自身来创建和持有它的对象实例,把对实例的创建权和管理权都控制在自己手中,以便控制 ...

  8. tomcat安全设置

    1.关闭服务器端口:server.xml默认有下面一行: <Server port="8005" shutdown="SHUTDOWN"> 这样允许 ...

  9. [itint5]单词变换

    http://www.itint5.com/oj/#42 基本上就是word ladder.直接来BFS,记录前驱. vector<string> transform(set<str ...

  10. R语言中的箱图介绍 boxplot

    画箱图的函数: boxplot()##help(boxplot)查询具体用法   图例的解释: 如下图,是两个简单的箱图. 中间的箱子的上下边,分别是第三,一个四分位数. 中间的黑线是第二四分位数(中 ...