LintCode-Search for a Range
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]
.
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
Solution:
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 ArrayList<Integer> searchRange(ArrayList<Integer> A, int target) {
ArrayList<Integer> res = new ArrayList<Integer>();
int start = -1, end = -1;
int p1 = 0, p2 = A.size()-1;
//find start point.
while (p1<=p2){
int mid = (p1+p2)/2; if (A.get(mid)==target){
if (mid==0 || A.get(mid-1)!=target){
start = mid;
break;
} else {
p2 = mid-1;
}
} else if (A.get(mid)>target)
p2 = mid-1;
else p1 = mid+1;
} //find end point.
p1 = 0;
p2 = A.size()-1;
while (p1<=p2){
int mid = (p1+p2)/2; if (A.get(mid)==target){
if (mid==A.size()-1 || A.get(mid+1)!=target){
end = mid;
break;
} else p1 = mid+1;
} else if (A.get(mid)>target)
p2 = mid-1;
else p1 = mid+1;
} res.add(start);
res.add(end); return res;
}
}
LintCode-Search for a Range的更多相关文章
- LintCode Search For a Range (Binary Search)
Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较. 循环终止条件: 最后剩两数比较(while(left + 1 < righ ...
- [OJ] Search for a Range
LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- 【LeetCode】34. Search for a Range
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
随机推荐
- 教你快速写出多线程Junit单元测试用例 - GroboUtils
摘自: http://mushiqianmeng.blog.51cto.com/3970029/897786/ 本文出自One Coder博客,转载请务必注明出处: http://www.coderl ...
- MITM to crack Https connections
Everybody knows that https is http over SSL, and https is a secure way for protecting confidential d ...
- c programming language ___ 5_2.c
#include <stdio.h> #include <ctype.h> #define BUG printf("here!bug!\n"); int g ...
- rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...
- CSS代码使纯英文数字自动换行
当一个定义了宽度的块状元素中填充的全部为纯英文或者纯数字的时候,在IE和FF中都会撑大容器,不会自动换行并且当数字或者英文中带有汉字时,会从汉字处换行,而纯汉字却可以自动换行.这个问题如何解决?先来认 ...
- Bitmap.Config 详解
前言 Android是一个内存相当吃紧的系统,那么在做程序的过程中使用内存就需要相当谨慎,而我们接触最大的大对象估计就是Bitmap了,那么下面就根据Bitmap.Config值的介绍来看下Bitma ...
- a标签至于flash之上的时候,IE浏览器无法点击连接的问题
<a style="display: block;height: 131px;position: absolute;z-index: 999;width: 222px;backgrou ...
- dedecms 根据key取得联动类型(enum)值
---恢复内容开始--- //$key:如城市的ID,$enum_file data/enums中的文件 function get_enum_data($key, $enum_file) { ...
- jquery ajax跨域请求详解
本文章来给大家详细jquery中的ajax跨域请求, 在JQuery对于Ajax的跨域请求有两类解决方案,不过都是只支持get方式.分别是JQuery的jquery.ajax jsonp格式和jque ...
- ASP.NET MVC4学习笔记之Controller激活的扩展
一. 为什么要进行扩展 在前面的分析中,我们知道默认的Controller激活系统只能实例化无参构造函数的Controller类型,但在某些情况一下,我们希望某些服务的实例能够自动注入到Control ...