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 ...
随机推荐
- Oracle_双机备份
1.dataguard http://jingyan.baidu.com/article/f96699bb956ef2894e3c1b39.html http://blog.itpub.net/262 ...
- 聊天界面之气泡文本cell(二)使用Autolayout
聊天界面主要是cell的动态高度计算和效率的问题,参考网上的两篇文章: 1.优化UITableViewCell高度计算的那些事 http://www.cocoachina.com/ios/20150 ...
- PBcR - 纠错及组装算法
单分子测序reads(PB)的混合纠错和denovo组装 我们广泛使用的PBcR的原始文章就是这一篇 原文链接:Hybrid error correction and de novo assembly ...
- Validate Binary Search Tree [LeetCode]
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 樱花漫地集于我心,蝶舞纷飞祈愿相随---总结 适者:survival of the fittest 适者:survival of the fittest
编程什么的最讨厌了,总是忘记一些乱七八糟的,看起来并没有什么乱用的,比如(::“<>{}, 还有交作业的时候总是忽略大小写<(▰˘◡˘▰)> 马马虎虎莫名其妙就错了,其实大小写 ...
- Ruby学习笔记
#!/usr/bin/ruby puts "Hello, Ruby, what is your name?" $name = STDIN.gets puts "Hi, I ...
- c# 配置文件之configSections配置(三)
使用IConfigurationSectionHandler配置configSections ·步骤1:定义一个实体类 using System; using System.Collections.G ...
- 简单研究Android View绘制二 LayoutParams
2015-07-28 17:23:20 本篇是关于LayoutParams相关 ViewGroup.LayoutParams文档解释如下: LayoutParams are used by views ...
- 没有纳入spring管理的类如何注入spring管理的对象
spring 如何在普通类中调用注入的对象? spring 在Thread中注入@Resource失败,总为null~解决 springmvc 注入总是空指针异常? 以上的几个问题就是我在项目中遇到的 ...
- [计算机、网络相关历史]unix简史
本文2001年由台湾“网络农夫”所写,其人生平不祥,此文受鸟哥大力推崇,两人应该相识.文章写得很不错,应该是查了很多资料整理而成的,美中不足的是好多语句不通顺,国考语文绝对不及格,哈哈. 0.我的准备 ...