leetcode — search-for-a-range
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/search-for-a-range/
*
* Created by lverpeng on 2017/7/14.
*
* 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].
*
*/
public class SearchRange {
/**
* 查找target在有有序数组中的起始位置
*
* 先找左边界,普通二分查找是和target比较,如果相同就返回,这里小于等于num[mid],如果是等于num[mid]也是收缩右边,最后得到的就是左边界
* 右边界同上
*
* @param num
* @param target
* @return
*/
public int[] search (int[] num, int target) {
int left = 0;
int right = num.length - 1;
int mid = 0;
while (left <= right) {
mid = (left + right) / 2;
if (num[mid] >= target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
int targetLeft = left;
left = 0;
right = num.length - 1;
while (left <= right) {
mid = (left + right) / 2;
if (num[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
int targetRight = right;
if (target != num[targetLeft] || target != num[targetRight]) {
targetLeft = targetRight = -1;
}
int[] result = new int[2];
result[0] = targetLeft;
result[1] = targetRight;
return result;
}
public static void main(String[] args) {
SearchRange searchRange = new SearchRange();
int[] arr = new int[]{5, 7, 7, 8, 8, 10};
System.out.println(Arrays.toString(searchRange.search(arr, 8)));
}
}
leetcode — search-for-a-range的更多相关文章
- 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 (TODO)
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. ...
随机推荐
- Django路由分配及渲染
一.url路由分配 1.url配置的本质是将浏览器传递过来的路径和参数与服务器中的视图向匹配,并返回浏览器相应的视图函数. 2.url路径path规则 path(route, view, kwargs ...
- 在Linux下部署mysql时,使用group by碰到的问题
mysql使用group by 的时候报错,错误信息如下: 1055:ER_WRONG_FIELD_WITH_GROUP: Expression #2 of SELECT list is not in ...
- 如何安装ubuntu系统
https://www.cnblogs.com/Chinasf/archive/2010/05/06/1728840.html [Ubuntu 下挂ISO到虚拟光驱的方法] 各种方法参考如下论坛 ...
- 用Python对html进行编码
原地址:https://stackoverflow.com/questions/275174/how-do-i-perform-html-decoding-encoding-using-python- ...
- linux shell 变量子串
linx变量子串 在本例子中,变量 test=https://www.//cnblogs./com//jjmaokk/p/10135401.html 1,${#parameter} 返回变量$para ...
- spring aop 的代理工厂
参考 https://docs.spring.io/spring/docs/4.3.11.RELEASE/spring-framework-reference/htmlsingle/#aop-unde ...
- Python TypeError: 'module' object is not callable 原因分析
今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...
- echarts 调整图表 位置 的方法
###内部图表大小是与div容器大小位置相关的,如果想调整图表大小位置,调整div的属性就可以了### ###如果是想调整图表与div间上下左右留白,则设置grid属性就可以了### 如图所示: 具体 ...
- JAVA RSA非对称加密详解[转载]
一.概述1.RSA是基于大数因子分解难题.目前各种主流计算机语言都支持RSA算法的实现2.java6支持RSA算法3.RSA算法可以用于数据加密和数字签名4.RSA算法相对于DES/AES等对称加密算 ...
- redis & memcache 性能比较
redis和memcache非常像的,都是key,value的方式,将数据存放内存中.最近在学习redis,在网上看了一些这方面的资料,有三种观点: redis读写内存比memcache快 memca ...