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. ...
随机推荐
- 第五周助教工作总结——NWNU李泓毅
第五周助教总结 注:因第四次实验安排两个标准时间完成,因此本周未提交完整作业. 本周心得: 第四次实验进行过半,八组同学都在实验课上进行了一次中期总结,并形成书面总结在微信群中讨论. 根据各组同学的中 ...
- Cortext-A7_i.MX 6ULL——多模式DDR控制器(MMDC)
1.概述 i.MX 6ULL系列芯片的MMDC是一个多模式DDR控制器,支持DDR3/DDR3Lx16和LPDDR2x16的存储类型,MMDC是可配置,高性能,优化的内存控制器. 注:DDR3/DDR ...
- uboot常用的函数
http://blog.csdn.net/ooonebook/article/details/53206623 以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为 ...
- while read line 查找指定日期的文件夹
#!/bin/bash dir=/root/tmp targetdir=/root/tmp/tmp filenametxt=/tmp/filename.txt commandtxt=/tmp/comm ...
- js实现全屏和缩放
/** * @description 简单的浏览器检查结果. * `webkit` * webkit版本号,如果浏览器为非webkit内核,此属性为`undefined`. * `chrome` * ...
- c刷题
1.转义字符: C中定义了一些字母前加 "\" 来表示常见的那些不能显示的ASCII字符,如\0 空字符,\r 回车, \n换行等,就称为转义字符,因为后面的字符,都不是它本来的A ...
- jsp获取当前项目跟路径
在jsp中获取当前项目的根路径: <% String basePath = request.getScheme() + "://"+ request.getServerNam ...
- html、css基础整理
1.块元素与行内元素之间的转换: HTML可以将元素分类方式分为行内元素.块状元素和行内块状元素三种.这三者是可以互相转换的,使用display属性能够将三者任意转换: (1)display:inli ...
- Mac再建管理员帐户
在Mac上添加用户没有成功, 把原来的管理员的用户群组修改后, 账户类型成为普通用户. 百度如何重新添加管理员: Mac上修改用户名后,无法用管理员账号和密码登录. 按照超过方法下去, 出现问题: 1 ...
- Virtual Networking
How the virtual networks used by guests work Networking using libvirt is generally fairly simple, an ...