题目描述:

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. 【NHibernate】配置- sql打印

    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <pr ...

  2. Mac环境下装node.js,npm,express;(包括express command not found)

    1. 下载node.js for Mac 地址: http://nodejs.org/download/ 直接下载 pkg的,双击安装,一路点next,很容易就搞定了. 安装完会提醒注意 node和n ...

  3. 让<未将对象引用到实例>见鬼去吧!

    未将对象引用到实例,即NullReferenceException异常,我相信这是c#编程中最常见的错误之一,至少我在做项目的过程中,有很多时候都会抛出这个异常.每当这个异常出现的时候,我都会头皮一紧 ...

  4. bnuoj 1057 函数(模拟)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1057 [题意]:给定x的值,带入f(x)求函数值 [题解]:注意第一个数的符号可能是'+',这里把我 ...

  5. 使用Redis构建简单的ORM

    Reids相关的资料引用 http://www.tuicool.com/articles/bURJRj [Reids各种数据类型的应用场景] https://github.com/antirez/re ...

  6. 【转载】test和cmp比较

    标 题:test和cmp一个很菜很基础的话题! 作 者: FTBirthday 时 间:2003/05/19 01:14am 链 接:http://bbs.pediy.com 看过破解教程,都知道te ...

  7. Ehcache 整合Spring 使用页面、对象缓存(转载)

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  8. hdu 3389 Game 博弈论

    思路: 其本质为阶梯博弈; 阶梯博弈:博弈在一列阶梯上进行,每个阶梯上放着自然数个点,两个人进行阶梯博弈...     每一步则是将一个集体上的若干个点( >=1 )移到前面去,最后没有点可以移 ...

  9. hdu 2147 kiki's game 博弈论

    找规律的博弈论!! 很容易发现当n,m都为奇数时先手必败! 代码如下: #include<iostream> #include<stdio.h> #define I(x,y) ...

  10. linux ps查看进程命令

    linux ps查看进程命令ps命令作用:将某个时间点的程序运作情况撷取下来 实例: [root@linux ~]# ps aux [root@linux ~]# ps -lA [root@linux ...