Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置
问题描述:给定一个有序序列,找到指定元素的起始和结束位置。例如:1234555,5,起始4结束6
算法分析:其实就是一个二分查找的利用。但是特殊就在不是找到某个元素,而是找到下标。也就是在nums[mid]=target时,要分析mid的左右元素。
public int[] searchRange(int[] nums, int target)
{
if(nums == null || nums.length == 0)
{
return null;
}
int[] arr = {-1,-1};
binarySearch(nums, 0, nums.length - 1, target, arr);
return arr;
} public void binarySearch(int[] nums, int left, int right, int target, int[] arr)
{
int mid = (left + right)/2;
if(left > right)
{
return;
}
if(nums[left] == target && nums[right] == target)//特例
{
arr[0] = left;
arr[1] = right;
return;
}
if(nums[mid] == target)
{
int templ = mid, tempr = mid;
while(templ>=left && nums[templ]==target)
{
templ --;
}
arr[0] = templ+1;
while(tempr<=right && nums[tempr]==target)
{
tempr ++;
}
arr[1] = tempr-1;
}
else if(nums[mid] < target)
{
binarySearch(nums, mid + 1, right, target, arr);
}
else
{
binarySearch(nums, left, mid - 1, target, arr);
}
}
Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置的更多相关文章
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [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(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
随机推荐
- 《从零开始学Swift》学习笔记(Day 56)——命名规范Swift编码规范之命名规范
原创文章,欢迎转载.转载请注明:关东升的博客 程序代码中到处都是自己定义的名字,取一个有样并且符合规范的名字非常重要. 命名方法很多,但是比较有名的,广泛接受命名法有: 匈牙利命名,一般只是命名变量, ...
- 【IDEA】单元测试:项目中引入JUnit测试框架+Mock简单了解
一.Junit 使用和说明: 参考:单元测试第三弹--使用JUnit进行单元测试-HollisChuang's Blog http://www.hollischuang.com/archives/17 ...
- tortoisegit错误: disconnected - no supported authentication methods available(server sent: publickey)
修改小乌龟的 SSH客户端:
- 解决: ./netapp.bin: error while loading shared libraries: libcaffe.so.1.0.0: cannot open shared object file: No such file or directory 运行时报错(caffe)
caffe安装好后lib没有配置到/usr/lib或/usr/local/lib中,需手动配置: export LD_LIBRARY_PATH=/path_to_your_caffe/build/li ...
- windows下安装Composer提示缺少openssl的解决方法
在Windows环境下安装Composer(注:Composer要求PHP版本在5.3.2+),你可能会遇到这种安装失败的情况:出错信息是 "The openssl extension is ...
- [ngClass]、[ngStyle]的基本使用(转载)
1.ngStyle 基本用法 <div [ngStyle]="{'background-color':'green'}"></<div> 判断添加 & ...
- 什么是web接口
当我们在请求一个页面的时候,会显示服务器返回的资源,其中包含了HTML.CSS和JS,除此之外,服务器还可以返回图片.视频.字体和插件等类型的资源.这些资源全部由HTTP协议传输. 如果把HTTP协议 ...
- Hbase 学习笔记3----操作以及维护
一,基本命令: 建表:create 'table','t1','t2' 也可以建表时加coulmn的属性如:create 'table',{NAME => 't1', BLOOMFI ...
- PHP数组的创建
案例: 仔细看代码,PHP创建数组 <?php $names[0]='Peter'; $names[1]='Minot'; $names[2]='Smith'; echo $names[0].' ...
- Apache和Nigix
Apache , Nginx 是开源的HTTP服务器软件 HTTP服务器本质上也是一种应用程序--它通常运行在服务器之上,绑定服务器的IP地址并监听某一个tcp端口来接收并处理HTTP请 ...