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操作MySQL的常用函数
某些情况下(如html中),调用php的变量时,要给变量加{},若要使字符串变量加上引号,则还需要在{}外加引号 如: $sql="select * from admin where use ...
- GPIO相关知识
参考资料: 1. 维基百科GPIO 2. GPIO博客资料(一) 3. MMIO和PMIO 知识点: ● GPIO是General-purpose input/output的缩写,是一个在集成电路上的 ...
- Web API开发实例——对产品Product进行增删改查
1.WebApi是什么 ASP.NET Web API 是一种框架,用于轻松构建可以由多种客户端(包括浏览器和移动设备)访问的 HTTP 服务.ASP.NET Web API 是一种用于在 .NET ...
- onethink使用经验
1 建议随时从oschina上下载onethink的最新版本,如果你遇到了怎么都解决不了的问题,比如菜单管理自定义菜单,左侧二级菜单不显示的问题,好像有一个历史版本就是有bug,好像是1.1开始的一个 ...
- spring bean的初始化
scope:作用域 singleton prototype request session 默认为singleton lazy-init:default=false ,false ,tru ...
- Codeforces Round #262 (Div. 2)
A #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- 描述Linux运行级别0-6的各自含义
中文: 0: 关机模式 1:单用户模式<==破解root密码 2:无网络支持的多用户模式 3:有网络支持的多用户模式(文本模式,工作中最长使用的模式) 4:保留,未使用 5:有网络支持又x-wi ...
- 软件测试--测试Demo
视频地址(第二课时):https://pan.baidu.com/s/1gfLVC2n 软件安装好了! 软件默认的浏览器是火狐. 如果需要IE,chrome,都在前一篇的安装包里有. 测试结果 视频里 ...
- 20160908_Redis主从复制Replication
1.主从redis,安装配置都是一样的.下面开始从服务器的配置. 参考的网址为:http://yanliu.org/2015/08/27/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8 ...
- mysql数据库编码问题
http://www.cnblogs.com/sunzn/archive/2013/03/14/2960248.html 修改/etc/my.cnf 添加: [mysql] default-chara ...