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 ...
随机推荐
- 7.javascript如何调试代码
http://www.cnblogs.com/youring2/archive/2012/08/08/2624093.html
- MySQL Connector/C++ C++连接mysql
MySQL :: MySQL Connector/C++ Developer Guide :: 1 Introduction to Connector/C++ https://dev.mysql.co ...
- a标签的href和onclick
1.链接的onclick事件被先执行,href的事件后执行 2.就今天遇到的问题说一下,通过a标签的onclick事件实现点击后定位到指定的div,或者是执行其他函数(比如弹出artdialog等), ...
- 接口测试工具 — jmeter(header与cookie的添加)
1.header的添加 添加HTTP信息头管理器 填写header 2.添加cookie 添加HTTP Cookie管理器 添加cookie值
- HTTP 协议介绍
HTTP 协议规定了浏览器和服务器之间互相通信的规则. 请求协议: 规定了客户端发送给服务器的内容格式 响应协议: 服务器发送给客户端的内容格式 请求协议 请求协议格式: 请求行 多个请求头信息(属性 ...
- .net ASPxTreeList 使用手记
ASPxTreeList在使用ASPxGridViewExporter控件做导出时,如果指定文件名是中文时会乱码可以用以下方法解决: grvExporter为ASPxGridViewExporter控 ...
- 对比MySQL,你究竟在什么时候更需要MongoDB(转)
译文:对比MySQL,你究竟在什么时候更需要MongoDB 原文链接: When Should I Use MongoDB rather than MySQL (or other RDBMS): Th ...
- Python(数据库之表操作)
一.修改表 1. 修改表名 ALTER TABLE 表名 RENAME 新表名; #mysql中库名.表名对大小写不敏感 2. 增加字段 ALTER TABLE 表名ADD 字段名 数据类型 [完整性 ...
- Linux内核设计与实现——内核同步
内核同步 同步介绍 同步的概念 临界区:也称为临界段,就是訪问和操作共享数据的代码段. 竞争条件: 2个或2个以上线程在临界区里同一时候运行的时候,就构成了竞争条件. 所谓同步.事实上防止在临界区中形 ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第三步---编辑器(2)---更方便很多其它操作更像编辑器
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦.他说:随便写,第一别全然照搬代码:第二能够说 ...