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. ...
随机推荐
- cerr与cout区别
语言:C++ 一.简介 平常常会用的主要有三个:cout.cerr.clog,首先简单介绍下三者. 这三者在C++中都是标准IO库中提供的输出工具(至于有关的重载问题在此不讨论): cout:写到标准 ...
- HDU 6346 整数规划 (最佳完美匹配)
整数规划 Time Limit: 5500/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- linux时区和时间设置
1,修改时区 调整时区使用tzselect [root@lyn ~]# hwclock Tue Nov :: PM AST -0.198205 seconds [root@lyn ~]# tzsele ...
- [solution]JZOJ-5838 旅游路线
[solution] JZOJ-5838 旅游路线 Time Limits 1000ms,Memory Limits 128MB 题面 Description GZOI队员们到X镇游玩.X镇是一个很特 ...
- SqlServer 连接 相关。
sqlserver数据库连接池是一个客户端的东西.和sql server服务器无关. 各种provider默认的连接池大小不同. 比如:Ado.NET 中sqlserver 连接池默认的值是100. ...
- poj 1050 最大子矩阵
a11 a12 a13 a14 a15 a21 a22 a23 a24 a25 a31 a32 a33 a34 a35 a41 a42 a43 ...
- kubernets基础
1.定义和功能. 1.1定义:kubernets解释为舵手或者飞行员,以Borg为主衍生出. 1.2功能:自动装箱,自我修复,水平扩展,服务发现和负载均衡,自动发布和回滚. 密钥和配置管理,存储编排, ...
- css基础回顾
1.css选择器分类: id选择器,类选择器,通用选择器, 包含(后代)选择器——加入空格,用于选择指定标签元素下的后辈元素. 子选择器(大于符号)——用于指定标签元素的第一代子元素. 伪类选择器—— ...
- 搭建vue环境
1. 下载安装nodejs 截至2018-06-05 最新稳定版本为 8.11.2,直接 next ,不改目录. PS C:\Users\Administrator> node -v v8.11 ...
- 一次java Cpu占用过高的排查
某一个项目CPU占用率一直很高,经常在40%-50%之间,最近比较闲,就开始了排查工作. 1.通过 jstack命令输出进程的堆栈信息 jstack 2788 >C:\log.txt 将堆栈信息 ...