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. abstract 和 interfaces 的用法注意事项

    abstract :  abstract class calssname{} 1.abstract也可也修饰普通的类,这样的目的是为了防止用这个类来创建对象: 2.abstract中的abstract ...

  2. Show Users Assigned to a Specific Role

    In a previous post I showed you how to know what Roles are assigned to a specific user. But here is ...

  3. 如何找出component的注册路径

      SELECT DISTINCT REVERSE(LTRIM(SYS_CONNECT_BY_PATH(REVERSE(PORTAL_LABEL),                           ...

  4. nginx 编译安装

    一.安装nginx时必须先安装相应的编译工具yum -y install gcc gcc-c++ autoconf automakeyum -y install zlib zlib-devel ope ...

  5. php数组去重复代码

    php数组去重复数据示例,有时候获得的php数组中总是出现value重复的,使用下面的方法就可以去掉重复数据 以数字开头的重复数据如: Array (  [0] => 100  [k1] =&g ...

  6. Android Socket通信

    1.TCP: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  7. Index of my articles

    65:一个表格说明RelativeLayout中的几个重要属性[Written By KillerLegend] (2013-10-16 21:59) 64:win7修改软件[授权给…]后面的名称 ( ...

  8. 计算两条直线的交点(C#)

    PS:从其他地方看到的源码是有问题的.下面是修正后的 /// <summary> /// 计算两条直线的交点 /// </summary> /// <param name ...

  9. 关于 mvc 中 连字符 - 和下划线 _转换的问题。

     [潜水]大崔||哈尔滨(759666247) 10:02:16  如图   C#不承认 “-”[知府]古道今-湖北\xig<systemobject@126.com> 10:03:54  ...

  10. 第十三章 调试及安全性(In .net4.5) 之 验证程序输入

    1. 概述 本章介绍验证程序输入的重要性以及各种验证方法:Parse.TryParse.Convert.正则表达式.JavaScriptSerializer.XML Schemas. 2. 主要内容 ...