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 ...
随机推荐
- poj1008_Maya_Calendar
历法的转换. #include <stdio.h> #include <math.h> #include <string.h> ][]={ "pop&qu ...
- basis基本tcode
SM21 ST11 SM50 查看work process 使用情况 操作相关的查询功能 SM## 常用tcode SM01 锁定事务 SM04 用户清单 SM05 HTTP ...
- 在WP8项目中使用ARMASM
由于之前项目中某些密集运算优化的需要,涉及到ARMASM相关的内容, 所以有幸可以在此分享一下自己的经验. 先铺垫一些知识: 1. ARM处理器有两种指令ARM.THUMB, 在WP8下默认是THUM ...
- MariaDB之基于Percona Xtrabackup备份大数据库[完整备份与增量备份]
MariaDB之基于Percona Xtrabackup备份大数据库[完整备份与增量备份] 1.Xtrabackup的安装 percona-xtrabackup-2.2.3-4982.el6.x86_ ...
- TextField 限定只输入数字的方法
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementStri ...
- Hide a Subpage Using PeopleCode
There was a question asked on the forum on how to hid a subpage. There is no Peoplecode function to ...
- 通过 XML HTTP 加载 XML 文件
新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="02-通 ...
- VS2010 自动关闭的问题解决方法
分为如下几个解决方法: 没有安装VS2010的SP1,安装后,问题解决了 自定义设置,出现了不正确的情况,执行 devenv.exe /resetsettings 可以排除故障 使用 devenv.e ...
- Linux命令学习---目录
一.文件相关命令 1.文件显示 1)tail,head,more,less,cat,nl
- phpcms后台部分修改
1.后台登陆前提示信息取消及成功后提示信息取消. (1)后台登陆前提示信息取消 phpcms\modules\admin\classes\admin.class.ph ...