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 ...
随机推荐
- ubuntu-16.04.2-server-amd64.iso
w
- telnet --- no route to host solution "iptables -F " in the target machine
telnet --- no route to host solution "iptables -F " in the target machine
- Java 面向对象之构造函数和 this 关键字
构造函数 this 关键字 1. 构造函数 class Person { private String name; private int age; // 定义一个 Person 类的构造函数 Per ...
- .net ASPxTreeList 使用手记
ASPxTreeList在使用ASPxGridViewExporter控件做导出时,如果指定文件名是中文时会乱码可以用以下方法解决: grvExporter为ASPxGridViewExporter控 ...
- C++设计模式 -- 解析和实现
原文地址 http://c.chinaitlab.com/special/sjms/Index.html#a 导航目录 ※ 设计模式解析和实现之一-Factory模式 ※ 设计模式解析和实现之八-C ...
- CNI插件实现框架---以loopback为示例
以最简单的loopback插件作为实例,来分析CNI plugin的执行流程 // cni/plugins/loopback/loopback.go 1.func main() main函数只是简单地 ...
- R-CNN for Small Object Detection
R-CNN for Small Object Detection 文章方法概括 这篇文章主要讨论针对小目标的目标检测 文章为了证明:对传统R-CNN style的方法进行改进,可以用于小目标检测,并且 ...
- 升级系统到ubuntun到18.04后apt-get执行失败
系统升级到18.04后执行apt-get install的时候报错 root@zhf-maple:/home/zhf/桌面# apt-get install vim-sciptsE: 无法获得锁 /v ...
- Linux的进程/线程通信方式总结(转)
Linux系统中的进程通信方式主要以下几种: 同一主机上的进程通信方式 * UNIX进程间通信方式: 包括管道(PIPE), 有名管道(FIFO), 和信号(Signal) * System V进程通 ...
- 空基类优化empty base class optimization
1.为什么C++中不允许类的大小是0 class ZeroSizeT {}; ZeroSizeT z[10]; &z[i] - &z[j]; 一般是用两个地址之间的字节数除以类型大小而 ...