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 ...
随机推荐
- mysql创建索引/删除索引操作
-- 1.ALTER 创建索引 -- table_name表名,column_list列名,index_name索引名 -- 创建index索引 ALTER TABLE table_name ADD ...
- 微信读书App来了 小伙伴们快去占榜吧
微信读书App正式上线了,iOS版和Android版同时推出.届时将会出现像微信运动一样的霸榜小伙伴.资料显示,阅文集团成立于2014年1月,是腾讯文学和盛大文学联合成立的新公司.阅文集团成立后,会对 ...
- 0408-服务注册与发现-Eureka常用配置
一.概述 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_appendix ...
- git常用命令总结(转载)
Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新建代码库 # 在当前目录新建一个Git代码库 $ git in ...
- HTTP状态码集
1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非在某些试 ...
- go——切片(二)
切片是一种数据结构,这种数据结构便于使用和管理数据集合. 切片是围绕动态数组的概念构建的,可以按需自动增长和缩小. 切片的动态增长是通过内置函数append来实现的.这个函数可以快速且高效地增长切片. ...
- go——字典(二)
字典是一种数据结构,用于存储一系列无序的键值对. 字典是基于键来存储值.字典功能强大的地方是能够基于键快速检索数据. 键就像索引一样,指向与键关联的值. 1.内部实现 字典是一个集合,可以使用类似处理 ...
- UML类图几种关系的总结(转载 http://blog.csdn.net/tianhai110/article/details/6339565 )
http://blog.csdn.net/tianhai110/article/details/6339565
- EXSITS应该怎么用?
无论是做项目还是普通使用SQL,我们通常都会使用IN.因为很好理解,也很方便.但是,面对着多层查询嵌套,或者IN关键字里面的结果集数量巨大,查询的效率就会直线下降.这时候,我们应该用好EXSITS. ...
- Java程序设计专题