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. HDU1698 Just a Hook

    Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  2. HYSBZ 4197 寿司晚宴

    Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同 ...

  3. jquery ajax提交表单数据的两种方式

    http://www.kwstu.com/ArticleView/kwstu_201331316441313 貌似AJAX越来越火了,作为一个WEB程序开发者要是不会这个感觉就要落伍,甚至有可能在求职 ...

  4. 深入解析MySQL分区(Partition)功能

    自5.1开始对分区(Partition)有支持 = 水平分区(根据列属性按行分)= 举个简单例子:一个包含十年发票记录的表可以被分区为十个不同的分区,每个分区包含的是其中一年的记录. === 水平分区 ...

  5. 一机双mysql的安装和启动注意事项目

    ./configure --prefix=/usr/local/mysql5.1/ --with-mysqld-user=mysql --sysconfdir=/usr/local/mysql5.1/ ...

  6. 只能输入汉字js脚本

    <html> <head> <meta http-equiv="Content-Type" content="text/html" ...

  7. Mongodb c#增删改查

    写在前面 最近项目需要,就研究了下mongodb,也是为了快速上手,就自己弄了一个简单的例子,这里记录一下. Mongodb 传统的关系数据库一般由数据库(database).表(table).记录( ...

  8. 在Microsoft-IIS/10.0上面部署mvc站点的时候,出现404的错误

    写在前面 在家自己弄了一个项目,想部署在电脑上用手机来访问,总是出现404的错误.路由什么的没有写错啊,最后发现是映射程序的问题,在安装的时候iis很多功能没有安装,又将iis的其他没有安装的功能勾选 ...

  9. 三个css3趣玩小试

    http://jsbin.com/semeh/8 请使用chrome打开 1.类似于网易新闻客户端的loading效果,左边的圆圈 2.发散式心跳效果,右边的圆圈 3.youtub上,搜索进度条效果, ...

  10. Web SQL数据库

    Web SQL数据库:它是一个独立的规范,引入了一组使用SQL操作客户端数据库的API. openDatabase方法:这个方法使用现有的数据库或者新建的数据库创建一个数据库对象.如果数据库存在,op ...