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. ASP.NET MVC3 局部页面@RENDERBODY @RENDERPAGE@RENDERSECTION使用方法详细说明

    转载自:http://blog.163.com/wenchangqing_live/blog/static/173722309201211299817278/ asp.net mvc3局部页面使用方法 ...

  2. ubuntu设置屏幕亮度

    楼主的本本是acer4750g ,系统是ubuntu kylin 14.04 原 笔记本ubuntu14.04无法调节屏幕亮度 http://my.oschina.net/lhplj/blog/397 ...

  3. py替换掉换行符

    for line in file.readlines(): line=line.strip('\n')

  4. c++实现gray code(格雷码)

    今天别人问的一道题,强调用分治法实现 =.= 百度了一下格雷码,然后写了一下. 关于格雷码大家看百度的吧,特别详细,贴个图: 代码如下(header_file.h是我自己写的一个头文件,包括常见的ve ...

  5. Linux mount/unmount命令(转)

    格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有:-a 安装在/etc/fstab文件中类出的所有文件系统.-f 伪装mount,作出检查设备和目录的样子,但并不真正挂载文件系 ...

  6. struct和union分析实例

    1.#include <stdio.h>#include <malloc.h>typedef struct _soft_array{    int len;    int ar ...

  7. zabbix 汉化

    zabbix2.x的版本自带汉化,3.x的版本也可以通过修改配置文件强制使用自带的汉化,但是不管哪种,翻译的精准度令人费解:偶然发现一个专门翻译zabbix的网站https://www.zabbix. ...

  8. wifi共享小工具

    MainForm.cs: using System;using System.Collections.Generic;using System.ComponentModel;using System. ...

  9. mingw32-g++.exe: *: No such file or directory错误解决方法

    初次使用CodeBlocks,好不容易把环境配好, 编译没有错误了,但是程序并不生成exe,提示以下问题: mingw32-g++.exe: /W3: No such file or director ...

  10. iOS-使用Xcode拉伸图片

    如果要制作一个类似于QQ消息气泡的图片,该如何制作呢?android中可以使用.9图片指定图片中的某一部分拉伸,那iOS中类似的功能要如何实现呢,Xcode提供了类似的功能.具体步骤如下: 1.选择需 ...