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 ...
随机推荐
- Oracle Database Documentation
Oracle数据库的发展简史 ORDBMS对象-关系数据库管理系统 Oracle Schema Objects Oracle Schema Objects——Tables——Overview of T ...
- 【Python算法】列表中的 append 比 insert 效率高的实质
append 与 insert 对比: # append 操作 >>> count = 10**5 >>> nums = [] >>> for i ...
- 模块化之SeaJS(一)
模块化(之SeaJS) 刚接触的童鞋可能会有很多疑惑,比喻:什么是模块?模块的目的是干嘛呀?怎么样实现模块化呢? 不要急,博主正是带着这三个问题来写这篇文章的. 一,什么是模块化? 在前端开发领域,一 ...
- 从库函数解析STM32地址映射
STM32的存储映射是靠基地址和地址偏移实现的. 32位的M3有4GB的寻址空间,其中用于片上外设的有512MB,基地址为0x40000000. M3各外设基地址,包括片上外设.片上静态RAM和FLA ...
- SSDT表概念具体解释
SSDT 的全称是 System Services Descriptor Table,系统服务描写叙述符表. 这个表就是一个把 Ring3 的 Win32 API 和 Ring0 的内核 API 联系 ...
- 【MonogDB】The description of index(二) Embedded and document Index
In this blog, we will talk about another the index which was called "The embedded ". First ...
- Java基础—内部类(转载)
转载自:java中的匿名内部类总结 在Java中,可以将一个类定义在另一个类里面或者一个方法里面,这样的类称为内部类.广泛意义上的内部类一般来说包括这四种:成员内部类.局部内部类.匿名内部类和静态内部 ...
- begoo——对象的CRUD操作
如果已知主键的值,那么可以使用这些方法进行CRUD操作 对object操作的四个方法Read/Insert/Update/Delete o := orm.NewOrm() user := new(Us ...
- CNN学习笔记:卷积运算
CNN学习笔记:卷积运算 边缘检测 卷积 卷积是一种有效提取图片特征的方法.一般用一个正方形卷积核,遍历图片上的每一个像素点.图片与卷积核重合区域内相对应的每一个像素值乘卷积核 .内相对应点的权重,然 ...
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...