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. ...
随机推荐
- Python从入门到精通之Sixth!
补充:enumerate 函数用于遍历序列(元组tuple.列表list.字典dict)中的元素以及它们的下标: >>> for i,j in enumerate(('a','b', ...
- jsonp 简单封装
import originJSONP from 'jsonp' // 引入 jsonp 模块 // 对外暴露方法 jsonp // 通常传给服务端的 url 地址带参数 设计目的是希望有纯净的 url ...
- oracle存储过程 out cursor
create or replace procedure BUILDEMPLID(emp_cursor out sys_refcursor) is n_emplid number; n_emplid1 ...
- vi/vim 三种模式的操作
来源:http://www.runoob.com/linux/linux-vim.html ps:刚刚进入vi/vim 是命令模式 一.命令模式 i 切换到输入模式,以输入字符. x 删除当前光标所在 ...
- Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学
https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...
- jtag、在线仿真器
指令集模拟器 1.部分集成开发环境提供了指令集模拟器,可方便用户在PC机上完成一部分简单的调试工作,但是由于指令集模拟器与真实的硬件环境相差很大,因此即使用户使用指令集模拟器调试通过的程序也有可能无法 ...
- How Does Closure Work in Javascript?
Simply, closure is the scope that it can visite and operate the variables outside of the function wh ...
- Appium之Android功能脚本
Android功能脚本 注:这里只写了登录和退出功能,以下不提供app的包名,下面我使用的是线上包 准备:1.Eclipse的Java环境:2.Appium环境:3.Android真机一台. 创建一个 ...
- js打断点
F12打开调试器 资源sources 找到就是文件 选中需要打断点的行 获得段短点的值:将断点向后执行一步(页面提示的桥状小图标),然后选中上一步需要打断点的值,悬浮在上 ...
- GodMode
将“GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}”(不含引号)复制过去,保存即可.