leetcode -- Search for a Range (TODO)
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]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
思路:
使用二分搜索找到target的idx,然后查看该idx的左右确定范围。
算法复杂度:
平均情况下是O(lgn);
最坏情况下数组中所有元素都相同O(n);
public class Solution {
public int[] searchRange(int[] A, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int idx = binarySearch(A, target);
int len = A.length;
int[] results = null;
if(idx == -1){
results = new int[]{-1, -1};
} else{
int l = idx;
int r = idx;
while(l >= 0 && A[l] == target){
l--;
}
l++; while(r < len && A[r] == target){
r++;
}
r--;
results = new int[]{l, r};
}
return results;
} public int binarySearch(int[] A, int target){
int len = A.length;
int l = 0, r = len - 1;
while(l <= r){
int mid = (l + r) / 2;
if(target == A[mid])
return mid; if(target > A[mid]){
l = mid + 1;
} else {
r = mid - 1;
}
} return -1;
}
}
google了下,要保证最坏情况下时间复杂度为O(lgn):进行两次二分搜索确定左右边界
leetcode -- Search for a Range (TODO)的更多相关文章
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode Search for a Range (二分查找)
题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- 告别where 1=1 最佳方案分享
已经有2年没有用过where 1=1了,没想到换了家公司后,又让我看到了它.在网络上面搜索了一下,发现没有人提供一个比较好的方案来解决这一问题.很多人说可以让数据库的优化机制去处理,但是,我想对于大部 ...
- Motan学习开篇
你已经走到这里了,后面只要耐心的走下去就行了. --佚名 入职新公司以后,公司使用dubbo框架,简单的照葫芦画瓢之后,也算是入手了,但是其中内部的实现的机制一概不懂.我单纯的有种好奇心,觉得每个 ...
- Android - LayoutInflater
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Java in ACM/ICPC
目录 Java在ACM/ICPC中的特点 在ACM/ICPC中使用Java需要注意的问题 Java与高精度计算 1.Java在ACM/ICPC中的特点 Java的语法和C++几乎相同 Java在执行计 ...
- 虚拟化技术对比:Xen vs KVM
恒天云:http://www.hengtianyun.com/download-show-id-68.html 一.说明 本文主要从功能方面和性能方面对Xen和KVM对比分析,分析出其优缺点指导我们恒 ...
- 使用PowerDesigner 设计SQL Server 数据库
工具: Sybase PowerDesigner 12.5 Microsoft SQL Server 2005 第一步:概念数据模型 打开PowerDesigner 软件,设计“概念数据模型”(Co ...
- Spark SQL概念学习系列之Spark SQL的简介(一)
Spark SQL提供在大数据上的SQL查询功能,类似于Shark在整个生态系统的角色,它们可以统称为SQL on Spark. 之前,Shark的查询编译和优化器依赖于Hive,使得Shark不得不 ...
- 我的github
我的github:先来贴个图~ 这是我的github,新建了第一个repository,默认路径是aokoqingiz/code. 然后是里面的文件~ 里面有一个readme.txt,是我对这个r ...
- protobuf 作为配置文件
公司每个project代码中,都有一个Config类,作为模块启动的配置.其实现如下 struct Config { int num; char * file_name; int load_from_ ...
- UVALive 7456 Least Crucial Node (并查集)
Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...