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

解题思路:

看到O(log n) 几乎可以肯定是二分查找的思路,题目不是特别难的那种,仔细想想就想出来了,JAVA实现如下:

	static public int[] searchRange(int[] nums, int target) {
int[] result = new int[2];
result[0] = result[1] = -1;
int left = 0, right = nums.length - 1;
while (left <= right) {
if (nums[(left + right) / 2] > target)
right = (left + right) / 2 - 1;
else if (nums[(left + right) / 2] < target)
left = (left + right) / 2 + 1;
else {
result[0] = result[1] = (left + right) / 2;
while (target != nums[left]) {
if (target > nums[(result[0] + left) / 2])
left = (result[0] + left) / 2 + 1;
else {
result[0] = (result[0] + left) / 2;
left++;
}
}
result[0] = left;
while (target != nums[right]) {
if (target < nums[(result[1] + right) / 2])
right = (result[1] + right) / 2 - 1;
else {
result[1] = (result[1] + right) / 2;
right--;
}
}
result[1] = right;
break;
}
}
return result;
}

Java for LeetCode 034 Search for a Range的更多相关文章

  1. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

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

  2. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  3. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  4. Java for LeetCode 081 Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  5. [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 ...

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

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

  8. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  9. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. Session的异常

    既然这一天就这么废了,那就多说一些吧!其实session也是有潜在的问题的.Session销毁的三种情况: (1)超时:超过30分钟 (2)服务器非正常关闭,如果自己手动stop service而不是 ...

  2. Erlang练习题----shopping

    直接就上代码了: -module(shop). -export([cost/1,total/1]). cost(orange) -> 5; cost(newspaper) -> 8; co ...

  3. startx启动过程分析

    http://blog.csdn.net/hustwarhd/article/details/3069066 JiananHe 09/19/2008 目录 xinit 1.1      功能 1.2  ...

  4. Java线程中run和start方法的区别

    http://bbs.csdn.net/topics/350206340 Thread类中run()和start()方法的区别如下:run()方法:在本线程内调用该Runnable对象的run()方法 ...

  5. #pragma预处理实例

    1.#include <stdio.h>#if defined(ANDROID20)    #pragma message("Compile Android SDK 2.0... ...

  6. Why Deep Learning Works – Key Insights and Saddle Points

    Why Deep Learning Works – Key Insights and Saddle Points A quality discussion on the theoretical mot ...

  7. CodeForces 705A(训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/A 从第三个输出中可看出规律, I hate that I love that I hate it ...

  8. SQL 基本语句

    1.修改sa账户密码 在查询分析器中执行如下语句: sp_password Null,'teracypwd','sa' 把SA的密码设为"teracypwd" 执行成功后有&quo ...

  9. http https 区别

    HTTPS和HTTP的区别 一.https协议需要到ca申请证书,一般免费证书很少,需要交费.  二.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议.  三. ...

  10. html中table的画法及table和div的区别

    最近项目中,根据客户的要求需要在页面上展示各种报表什么的,各种表格的都会出现.这里也将table的画法,做一下总结.办法虽笨但很实用.这也是从高人那里学来的,总之是屡试不爽啊.就以下面的表格为例. 若 ...