【leetcode】Array——Search for a Range(34)
2016-02-26 13:31
381 查看
题目:
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
For example,
Given
return
思路:还是经典的binary search,如果nums[mid]==target,从mid出发,向左右遍历,找范围。但要注意可能会出现out of array range
代码:
改进思路1:用binary search查找第一个大于或者等于target的位置,则结果为[ firstGreaterEqual(target), firstGreaterEqual(target+1)-1]
改进思路2:分别用binary search查找target第一次出现的位置和最后出现的位置,binary内部的细节很重要,主要是当nums[mid]==target的时候,是向左还是向右,决定了是第一次出现还是最后出现的位置。
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].
思路:还是经典的binary search,如果nums[mid]==target,从mid出发,向左右遍历,找范围。但要注意可能会出现out of array range
代码:
public int[] searchRange(int[] nums, int target) { int left=0,right=nums.length-1; int [] results = new int [2]; while(left<=right){ int mid = (left+right)/2; if(nums[mid]==target){ //search left range int lefttemp = mid; while(nums[lefttemp]==target) { lefttemp--; if(lefttemp==-1) break; } results[0]=lefttemp+1; //search right range int righttemp = mid; while(nums[righttemp]==target) { righttemp++; if(righttemp==nums.length) break; } results[1]=righttemp-1; return results; } if(target<nums[mid]) right = mid-1; else left = mid +1; } //如果没有在循环里面内return,说明没有匹配到target,返回[-1,-1] results[0]=results[1]=-1; return results; }虽然,AC了,但是细想时间复杂度并不是 O(log n)。例如,[1,1,1,1,1,1,1,1],这时候是O(n)。
改进思路1:用binary search查找第一个大于或者等于target的位置,则结果为[ firstGreaterEqual(target), firstGreaterEqual(target+1)-1]
public class Solution { 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}; } //find the first number that is greater than or equal to target. //could return A.length if target is greater than A[A.length-1]. //actually this is the same as lower_bound in C++ STL. private static int firstGreaterEqual(int[] A, int target) { int low = 0, high = A.length; while (low < high) { int mid = low + ((high - low) >> 1); //low <= mid < high if (A[mid] < target) { low = mid + 1; } else { //should not be mid-1 when A[mid]==target. //could be mid even if A[mid]>target because mid<high. high = mid; } } return low; } }leetcode链接:https://leetcode.com/discuss/19368/very-simple-java-solution-with-only-binary-search-algorithm
改进思路2:分别用binary search查找target第一次出现的位置和最后出现的位置,binary内部的细节很重要,主要是当nums[mid]==target的时候,是向左还是向右,决定了是第一次出现还是最后出现的位置。
public class Solution { public int[] searchRange(int[] nums, int target) { int[] result = new int[2]; result[0] = findFirst(nums, target); result[1] = findLast(nums, target); return result; } private int findFirst(int[] nums, int target){ int idx = -1; int start = 0; int end = nums.length - 1; while(start <= end){ int mid = (start + end) / 2; if(nums[mid] >= target){ end = mid - 1; }else{ start = mid + 1; } if(nums[mid] == target) idx = mid; } return idx; } private int findLast(int[] nums, int target){ int idx = -1; int start = 0; int end = nums.length - 1; while(start <= end){ int mid = (start + end) / 2; if(nums[mid] <= target){ start = mid + 1; }else{ end = mid - 1; } if(nums[mid] == target) idx = mid; } return idx; }leetcode链接:https://leetcode.com/discuss/52701/easy-java-o-logn-solution点击打开链接
相关文章推荐
- Java继承
- Toad 使用中遇到的问题
- Android广播机制
- 由StreamWriter.WriteLine 引发对C#多线程的深入思考(一)
- java的继承练习
- java注解XML
- 转:【工欲善其事必先利其器】—Entity Framework实例详解
- leetcode 313. Super Ugly Number
- Android开源项目集合
- java 字符串初探(1)
- 自定义网站的icon和收藏夹图标
- linux 部署tomcat 搭建nginx
- Html5 Egret游戏开发 成语大挑战(八)一般性二级页面处理
- NSDate相关
- AnimatedExpandableListView,解决你所有对于ExpandableListView的疑惑
- 开创不是那么简单
- Structs2过滤器拦截器
- 轻量级 Linux下ssh破解工具
- JSON
- Form表单利用Jquery Validate验证以及ajax提交