34. 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].
链接: http://leetcode.com/problems/search-for-a-range/
题解:
考察Binary Search的结束条件。 使用两次Bianry Search,一次搜索左边界,一次搜索右边界,最后需要比较一下左边界是否 <= 右边界,假如条件不成立则target不在数组中。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = {-1, -1};
if(nums == null || nums.length == 0)
return res;
int lolo = 0, lohi = nums.length - 1, hilo = 0, hihi = nums.length - 1;
while(lolo <= lohi) { //try find left end
int mid = lolo + (lohi - lolo) / 2;
if(nums[mid] < target)
lolo = mid + 1;
else
lohi = mid - 1;
}
while(hilo <= hihi) { //try find right end
int mid = hilo + (hihi - hilo) / 2;
if(nums[mid] > target)
hihi = mid - 1;
else
hilo = mid + 1;
}
if(lolo <= hihi) { //if target exist
res[0] = lolo;
res[1] = hihi;
}
return res;
}
}
二刷:
Java:
要使用两次binary search来分别搜索target值的最左端和最右端。最后需要判断一下求出来的left 是否<= right,否则 [1], 1这样的test case会过不了
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = {-1, -1};
if (nums == null || nums.length == 0) {
return res;
}
int lo = 0, hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (target > nums[mid]) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
int left = lo;
lo = 0;
hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (target >= nums[mid]) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
int right = hi;
if (left <= right) {
res[0] = left;
res[1] = right;
}
return res;
}
}
三刷:
这回写得比较奇怪。原理都一样,都是使用两次binary search,但是改变了一些变量和条件。
要注意搜索左边界时,遇到mid = target时我们hi = mid - 1,最后返回的边界index是lo。搜索右边界时,遇到mid = target我们lo = mid + 1,最后返回的边界index是hi。也需要判断一下没有搜索到的case,这就是代码里的两个独立if语句的作用。
Java:
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = {-1, -1};
if (nums == null) return res;
int lo = 0, hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] < target) lo = mid + 1;
else hi = mid - 1;
}
if (lo > nums.length - 1 || nums[lo] != target) return new int[] {-1, -1};
res[0] = lo;
lo = 0;
hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > target) hi = mid - 1;
else lo = mid + 1;
}
if (hi < 0 || nums[hi] != target) return new int[] {-1, -1};
res[1] = hi;
return res;
}
}
Update:
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = {-1, -1};
if (nums == null) return res;
int lo = 0, hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] < target) lo = mid + 1;
else hi = mid - 1;
}
if (lo > nums.length - 1 || nums[lo] != target) return res;
res[0] = lo;
hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > target) hi = mid - 1;
else lo = mid + 1;
}
res[1] = hi;
return res;
}
}
34. Search for a Range的更多相关文章
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- 【LeetCode】34. Search for a Range
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
随机推荐
- table的边框线的设置
http://hi.baidu.com/weisuotang/item/a1d98ec298c0aa49a8ba9447 http://www.cnblogs.com/xinlei/archive/2 ...
- SQLIO Disk Subsystem Benchmark Tool
C:\Program Files (x86)\SQLIO>sqlio -? sqlio v1.5.SG -?: invalid option Usage: sqlio [options] [&l ...
- Eclipse 常用插件
/* ===================== Eclipse Color Theme 语法高亮 ======================== */ 安装方法同上,软件安装URL http:// ...
- Eclipse中查看JDK类库的源代码
在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...
- poll实现
struct pollfd { int fd; //当前描述符 short events; //进程关心的该描述符的事件 short revents; //返回 ...
- .NET序员的成长之路
- 纯JS文本比较工具
前段时间由于工作需要写了一个纯JS文本比较工具 在这里与大家分享下 算法有待优化,还希望大家多多指教 先上效果图: 奉上源码(把源码保存为html格式的文件就可以直接运行了): <!doctyp ...
- 【h5-egret】深入浅出对象池
最近看到对象池这一块的东西,是频繁创建和删除类型游戏优化性能的一个解决方案. 简单来讲对象池就是个数组,把不用的对象放进去,因为数组还保存了对象的引用,所以对象不会被回收,等需要用的时候再从数组中取出 ...
- cocos2dx中的设计分辨率与屏幕适配策略
1.首先明确几个概念: 设计分辨率:designResolution,即资源图片的设计尺寸,即美工给你的资源图片的大小,比如(641*964) 屏幕分辨率:又叫帧的大小,glview->setF ...
- AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(一)
随着IOS7由之前UI的拟物化设计变为如今的扁平化设计,也许扁平化的时代要来了,当然我们是不是该吐槽一下,苹果什么时候也开始跟风了,自GOOGLE和微软界面扁平化过后,苹果也加入了这一队伍. Aval ...