Search for a Range
来自 <https://leetcode.com/problems/search-for-a-range/>
Given a sorted array of integers, 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].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
题目解读:
给定一个有序数组nums和一个整数target,找出target在数组中的起始位置。
算法的时间复杂度为O(logn).
如果target在数组中没有找到,则返回[-1, -1]。
例如
给定数组[5, 7, 7, 8, 8, 10] 和target值8.返回[3, 4]。
解析:根据题意可知,由于算法的时间复杂度为O(logn),并且数组是有序数组,所以要使用二分查找。在二分查找结束后,根据找到的一个索引,再向两边扩充。
Java代码一(递归):
public class Solution { /** * 二分查找递归 * @param nums * @param target * @return */ public int[] searchRange1(int[] nums, int target) { int[] result = new int[2]; int low = 0; int high = 0; int location = binarysearch(nums, target, 0, nums.length-1); /** * 如果没有找到 */ if(-1 == location) { result[0]=-1; result[1]=-1; } else { /** * 如果找到其中的一个值 */ //向左扩充 for(low=location; low>=0; low--) { if(nums[low] != nums[location]) break; } //向右扩充 for(high=location; high<nums.length; high++) { if(nums[high] != nums[location]) { break; } } result[0] = low+1; result[1] = high-1; } return result; } /** * 递归二分查找算法 * @param nums * @param target * @param low * @param high * @return */ public int binarysearch(int[] nums, int target, int low,int high) { if(low>high) return -1; int mid = (low+high) /2; if(target == nums[mid]) return mid; else if(target > nums[mid]) return binarysearch(nums, target, mid+1, high); else return binarysearch(nums, target, low, mid-1); } }
递归性能:
Java代码二(非递归):
public class Solution { /** * 二分查找非递归 * @param nums * @param target * @return */ public int[] searchRange(int[] nums, int target) { int[] result = new int[2]; int low = 0; int high = nums.length-1; int mid = 0; /** * 二分查找找出序列中的一个target值 */ while(low<=high) { mid = (low+high) / 2; if(target == nums[mid]) break; else if(target < nums[mid]) high = mid-1; else low = mid+1; } /** * 如果没找到 */ if(low > high) { result[0] = -1; result[1] = -1; return result; } /** * 如果找到其中的一个值 */ //向左扩充 for(low=mid; low>=0; low--) { if(nums[low] != nums[mid]) break; } //向右扩充 for(high=mid; high<nums.length; high++) { if(nums[high] != nums[mid]) { break; } } result[0] = low+1; result[1] = high-1; return result; } }
非递归性能:
相关推荐
《leetcode-solutions》,刷算法题,需要有一定的英文阅读能力。。。
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
IDEA 插件,lettcode刷题,leetcode-editor7.4版本下载进行本地导入(直接将压缩包拖进IDEA即可)
Algorithm-leetcode-spider.zip,leetcode公司,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
leetcode 答案解析 golang解答
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为
970. 强整数对数运算function powerfulIntegers(x: number, y: number, bound: number): numb
leetcode-cheat 的发布 它是什么 ? 这是一个chrome 扩展,可以帮助您更高效地使用 leetcode。您可以从 重要: leetcode-cheat 现在只支持中文版。 也就是说不完全支持leetcode.com,但是你可以用leetcode-cn.com代替...
leetcode-helper-1.7.1
leetcode-tag-dynamic programming
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...
leetcode-tag-Tree
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
leetcode-tag-Stack
Algorithm-LeetCode-Solution-From-GuaZiDou.zip,Leetcode解决方案Gitbook,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
leetcode-tag-array
leetcode-链表笔记
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...