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

Example

Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

Solution:

 public class Solution {
/**
*@param A : an integer sorted array
*@param target : an integer to be inserted
*return : a list of length 2, [index1, index2]
*/
public ArrayList<Integer> searchRange(ArrayList<Integer> A, int target) {
ArrayList<Integer> res = new ArrayList<Integer>();
int start = -1, end = -1;
int p1 = 0, p2 = A.size()-1;
//find start point.
while (p1<=p2){
int mid = (p1+p2)/2; if (A.get(mid)==target){
if (mid==0 || A.get(mid-1)!=target){
start = mid;
break;
} else {
p2 = mid-1;
}
} else if (A.get(mid)>target)
p2 = mid-1;
else p1 = mid+1;
} //find end point.
p1 = 0;
p2 = A.size()-1;
while (p1<=p2){
int mid = (p1+p2)/2; if (A.get(mid)==target){
if (mid==A.size()-1 || A.get(mid+1)!=target){
end = mid;
break;
} else p1 = mid+1;
} else if (A.get(mid)>target)
p2 = mid-1;
else p1 = mid+1;
} res.add(start);
res.add(end); return res;
}
}

LintCode-Search for a Range的更多相关文章

  1. LintCode Search For a Range (Binary Search)

    Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较. 循环终止条件: 最后剩两数比较(while(left + 1 < righ ...

  2. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

  3. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  4. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

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

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

  6. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  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. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

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

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

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

随机推荐

  1. windows7安装远程服务器AD域管理工具

    目的:在win7上安装“远程服务器管理工具”,这样可以在客户端进行对服务器的AD域的操作,避免了远程登陆进服务器的麻烦. 前提条件:一般此工具只有管理员才具有有效使用权限,所以,在域administr ...

  2. centOS 6.4下安装中文输入法

    1.用root登录 ,或su root 2.yum install "@Chinese Support" 3.exit 4.回到桌面,system->preferences- ...

  3. final 的用法总结

    1.修饰成员变量 修饰普通变量 表明这个变量是一个常量,不可以修改这个变量的值,一般这样的变量的变量名都要大写 修饰引用变量 表明这个引用不能够指向别的对象了,只能够指向指定的这个对象 2.修饰方法 ...

  4. Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  5. php mkdir函数

    if(!is_dir($targetPath)){mkdir($targetPath, 0700); } is_dir 判断目录是否存在 mkdir 不能创建多级目录

  6. House of hello恶搞包之真假辨别

    第一次在我这边购买的客户都会问我:“是否保证正品?”确实,现在市面上Hoh的假货非常多,不过我想说的是,作为一位House of hello恶搞包的代理,我觉得买假货是非常可耻的,而且Abby自己也是 ...

  7. C puzzles详解【1-5题】

    第一题 #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) ,,,,,,}; int m ...

  8. linux修改登陆后进入的默认目录

    如将root登陆后进入的路径由/root改为/opt/FriendlyARM/linux/u-boot-mini6410修改/etc/pssswd 修改行 root:x:0:0:root:/root: ...

  9. PHPCMS建站经验分享

    在这里不对模型.模板设置.category,list,show等静态页面引入.配置文件(caches\configs\database.php 和 caches\configs\system.php) ...

  10. CMD怎样建立文件?

    一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...