Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较。

循环终止条件: 最后剩两数比较(while(left + 1 < right))。

循环结束后根据要求检查最后两个数(left/ right 和 target 比较)。

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 int[] searchRange(int[] A, int target) {
int[] array = new int[2];
array[0] = -1;
array[1] = -1;
if(A == null || A.length == 0) return array; int left = 0; int right = A.length - 1;
while(left + 1 < right){
int mid = (left + right) / 2;
if(A[mid] == target){
right = mid;
}
else if(A[mid] < target){
left = mid;
}
else if(A[mid] > target){
right = mid;
}
}
if(A[left] == target){
array[0] = left;
}
else if(A[right] == target){
array[0] = right;
}
else array[0] = -1; left = 0; right = A.length - 1;
while(left + 1 < right){
int mid = (left + right) / 2;
if(A[mid] == target){
left = mid;
}
else if(A[mid] < target){
left = mid;
}
else if(A[mid] > target){
right = mid;
}
}
if(A[right] == target){
array[1] = right;
}
else if(A[left] == target){
array[1] = left;
}
else array[1] = -1;
return array;
}
}

LintCode Search For a Range (Binary Search)的更多相关文章

  1. Lintcode: Insert Node in a Binary Search Tree

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  2. Lintcode: First Position of Target (Binary Search)

    Binary search is a famous question in algorithm. For a given sorted array (ascending order) and a ta ...

  3. [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II

    Closest Binary Search Tree Value  Given a non-empty binary search tree and a target value, find the ...

  4. [LeetCode] 35. Search Insert Position_Easy tag: Binary Search

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree

    二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...

  6. lintcode 中等题:unique Binary Search Tree 不同的二叉查找树

    题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 ...

  7. LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

    C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...

  8. [LeetCode] questions conclusion_ Binary Search

    Binary Search T(n) = T(n/2) + O(1)   =>    T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...

  9. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

随机推荐

  1. 用Git导出项目

    Git没有SVN的导出功能,不能像 svn export url 那样,将某个版本的代码导出为不带版本控制文件的文件夹.   Git提供了archive命令,可以把版本的文件流导出.   可以将Git ...

  2. select框内容的编辑、修改、添加、删除操作

    // 添加 function col_add() { var selObj = $("#mySelect"); var value="value"; var t ...

  3. Linux文件管理相关命令

    Linux文件管理相关命令   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以 ...

  4. C#中的多线程 - 基础知识

    原文:http://www.albahari.com/threading/ 文章来源:http://blog.gkarch.com/threading/part1.html 1简介及概念 C# 支持通 ...

  5. 02-编写第一个C语言程序

    本文目录 1.打开Xcode,新建Xcode项目 2.选择最简单的命令行项目 3.输入项目信息 4.选择一个用来存放C程序代码的文件夹 5.运行项目 说明:这个C语言专题,是学习iOS开发的前奏.也为 ...

  6. CSRF攻击原理以及防御

    一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...

  7. Thread-0" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.

    http://blog.csdn.net/jingshuigg/article/details/25001979 zookeeper.connect=localhost:2181改成zookeeper ...

  8. 纠错工具之 - Proovread

    BioInf-Wuerzburg/proovread - Github 主要是来解读 proovread 发表的文章,搞清楚它内在的原理. Proovread,这个工具绝对没有你想的那么简单,它引入了 ...

  9. miniui设置边框的方法

    if (field == "loginname") { if (record._id == 2) { e.cellHtml = ""; e.cellStyle ...

  10. selenium 安装与 chromedriver安装

    直接使用pip安装 pip slenium 用 Chrome 浏览器来测试   from selenium import webdriver browser = webdriver.Chrome() ...