LeetCode第[34]题(Java):Search for a Range
题目:搜索目标范围
难度:Medium
题目内容:
Given an array of integers nums sorted in ascending order, 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].
翻译:
给定一个按升序排序的整数,找到给定目标值的起始和结束位置。
您的算法的运行时复杂性必须按照O(log n)的顺序。
如果在数组中找不到目标,返回[-1,-1]。
我的思路:数组、有序、查找、lgn————》二分法
找到后用两个指针向两边移动,直到不等于,就是他的范围。
MyCode:
public int[] searchRange(int[] nums, int target) {
if (nums.length == 0) {
return new int[]{-1,-1};
}
int loc = binaryFind(nums, target);
if (loc == -1) {
return new int[]{-1,-1};
}
int start = loc;
int end = loc;
while (start-1 > -1 && nums[start-1] == nums[loc])
start--;
while (end+1 < nums.length && nums[end+1] == nums[loc])
end++;
return new int[]{start, end};
}
static int binaryFind(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low)/2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
我的复杂度:O(logN)+ O(N) = O(N)
因为后面的双向移动确实有可能将所有元素进行一次遍历,所以达不到O(logN)级别
编码过程中出现问题:
1、length 写成了 lengh;
2、high的初始值设置成了length,应该为length-1;
3、 while(start-1 > -1 && nums[start-1] == nums[loc]) 注意这个写法,如果将start--放入此处判断,那么最后的值就多减了1;
4、没找到的时候别忘了return [-1,-1]。
答案代码:
public int[] searchRange(int[] A, int target) {
int start = Solution.firstGreaterEqual(A, target);
if (start == A.length || A[start] != target) {
return new int[]{-1, -1};
}
return new int[]{start, Solution.firstGreaterEqual(A, target + 1) - 1};
}
private static int firstGreaterEqual(int[] A, int target) {
int low = 0, high = A.length;
while (low < high) {
int mid = low + ((high - low) >> 1);
//A[low] <= target < A[high]
if (A[mid] < target) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
答案复杂度:O(logN)
答案思路:在二分法的基础上做出了改进,即最后返回大于等于target的第一个数(所有target里最左边的那一个),
所以不仅当target>A[mid],并且target==A[mid]的时候,此时都应该向左边继续搜寻,
但是此时是令high = mid,而不是mid-1,是因为A[mid]<=target的时候,包括了A[mid]==target,此时如果取high = mid-1,则有可能将唯一的一个target给弄到右边去。
最后得到start,然后再将方法的target给+1再传入此方法,那么就能找到target右边的那一个数字的下标。
举个例子:
[1,2,3,5,5,9],target=5
首先定位到下标2——3,因为3<target,要找的点必定在右边:lo=mid+1
再定位下标4——5,因为5>=target,所以也要向左寻找:high=mid
。。。
扩展:我们也可以求的小于等于target的第一个数,即所有target最右边的那个数,那么当target>=A[mid]的时候,此时继续向右边搜索。。。
LeetCode第[34]题(Java):Search for a Range的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)
题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...
- LeetCode第[79]题(Java):Word Search(矩阵单词搜索)
题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word ...
- LeetCode第[33]题(Java):Search in Rotated Sorted Array
题目:在翻转有序中搜索 难度:Medium 题目内容: Suppose an array sorted in ascending order is rotated at some pivot unkn ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
随机推荐
- explorer.exe中发生未处理的win32异常
explorer.exe中发生未处理的win32异常的错误提示,是windows系统比较常见的错误事件,多数在开机遇到,也有在电脑使用过程中遇到. 了解explorer.exe进程 从百度百科了解到, ...
- ES6 Promise对象then方法链式调用
then()方法的作用是Promise实例添加解决(fulfillment)和拒绝(rejection)状态的回调函数.then()方法会返回一个新的Promise实例,所以then()方法后面可以继 ...
- pycharm-->github / github-->pycharm
一.pycharm -->发布到--> github:1.github 账号密码 https://github.com/2.git 下载安装 https://git-scm.com/3.配 ...
- Java 之 Servlet
JavaWeb 三大组件: Servlet, Filter, Listener. Servlet 的作用是处理请求,服务器会把接收到的请求交给 Servlet 来处理.在 Servlet 中通常需要: ...
- pycharm中选择python interpreter
pycharm中选择python interpreter pycharm中有两处地方需要选择python解释器: 一处是调试配置(edit configurations)处,这里选择python解释器 ...
- Flume日志收集 总结
Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据: 同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能力. (1) 可靠 ...
- Maven项目settings.xml的配置
原文地址 http://www.cnblogs.com/DreamDrive/p/5571916.html 在Maven中提供了一个settings.xml文件来定义Maven的全局环境信息.这个文件 ...
- ES6的十个新特性
这里只讲 ES6比较突出的特性,因为只能挑出十个,所以其他特性请参考官方文档: /** * Created by zhangsong on 16/5/20. */// ***********Nu ...
- begoo——对象的CRUD操作
如果已知主键的值,那么可以使用这些方法进行CRUD操作 对object操作的四个方法Read/Insert/Update/Delete o := orm.NewOrm() user := new(Us ...
- subprocess和struct模块
subprocess import subprocess obj = subprocess.Popen('dir',shell=True, stdout=subprocess.PIPE, stderr ...