LintCode Search For a Range (Binary Search)
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)的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- 二叉树系列 - 二叉搜索树 - [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 ...
- lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 ...
- LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- [LeetCode] questions conclusion_ Binary Search
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
随机推荐
- PHP匿名函数的使用
$dealer = array(); array_walk($dealer_id_arr,function($value) use(&$dealer) { $dealer[] = get_co ...
- XDocument 获取包括第一行的声明(版本、编码)的所有节点
XDocument保存为xml文件的方法如下: XDocument doc = new XDocument( new XDeclaration("1.0","UTF-8& ...
- 运维神器Chef简单介绍和安装笔记
首先大概解释一下Chef Chef有三个重要的概念:(如上图所示) 它们的合作关系大致是这样的, Workstation把资源或者说是一些要被运行的命令上传到Chef-Server上, Nodes自动 ...
- Oracle重做日志文件
一.联机重做日志的规划管理 1.联机重做日志 记录了数据的所有变化(DML,DDL或管理员对数据所作的结构性更改等) 提供恢复机制(对于意外删除或宕机利用日志文件实现数据恢复) 可以被分组管理 11 ...
- 初学者用div+css结构写网页的几个误区
1.用div+css结构制作静态html网页不等于彻底抛弃古老的table写法.之所以不建议用table来布局网页是因为在网页加载很慢的时候要等table结构加载完成才能看到网页,其次是table的布 ...
- 转!!log4j基础
log4j组件介绍 Log4j主要有三个组件: Logger:负责供客户端代码调用,执行debug(Object msg).info(Object msg).warn(Object msg).erro ...
- C++成员变量内存对齐问题,ndk下非对齐的内存访问导致BUS_ADRALN
同样的代码,在vs下运行正常,在android ndk下却崩溃: signal 7(SIGBUS),code 1 (BUS_ADRALN),fault addr 0xe6b82793 Func(sho ...
- C++指针之防不胜防
我们在使用指针时,经常会出现下面几种错误: 1) 内存分配未成功,却使用了它. 编程新手常犯这种错误,因为他们没有意识到内存分配会不成功.常用解决办法是,在使用内存之前检查指针是否为NULL.如果指针 ...
- Spring知识点
IOC: Inversion of Control 控制反转 ①自己控制→容器控制 ②控制具体实现→控制抽象(接口) DI:Dependency Injection 依赖注入 依赖于容器注入的对象 注 ...
- WCF初探-23:WCF中使用Message类(下)
前言 在上一篇WCF中使用Message类(上)中,文章介绍了WCF中使用Message类的基本知识和怎样创建消息,本文是承接上一篇文章,如果想要更好的阅读本文,请先阅读上一篇文章.在这篇文章中,我将 ...