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 ...
随机推荐
- PropertyDrawer 自定义属性绘图
public class PlayerAttributeExample : MonoBehaviour { //无滑块的属性 ; //特性限定,有滑块 [Range(, )] ; } Range特性的 ...
- 修改myeclipse的jsp模板
在myeclipse的安装目录下: C:\Users\Seeker\AppData\Local\MyEclipse Professional\plugins 找到com.genuitec.eclips ...
- cocos3.2中如何创建一个场景
1.可以将一些比较通用的东西放到Common.h中,这是一个.h文件,必须手动添加,且保证在classes目录里 #ifndef __COMMON_H__ #define __COMMON_H__ # ...
- tomcat 7 用mod_jk做 负载均衡
在Win7中使用apache为tomcat做负载均衡,各组件及版本如下: 两个tomcat v 7.0.57 一个apache v 2.2.14 一个mod_jk v 1.2.33(for windo ...
- ifame 跨域高度自适应
代码如下:var iframeids = ['memberIndexIframe','inquiryCenterIframe','everychinaBbsIframe']; var iframehi ...
- res/drawable目录下图片的Uri
http://liuyun025.iteye.com/blog/1280838 有时候,我们要用到res/drawable目录下的图片Uri,而这个Uri该如何生存呢?下面就是这Uri的生成方法: U ...
- uoj 67 新年的毒瘤 割点
题目链接: 题目 #67. 新年的毒瘤 问题描述 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树. 这个长着毒瘤的树可以用 nn 个结点 mm 条无向边的无向图表示.这个图中有 ...
- CSDN——【低调的草原狼】——Ext4.2学习目录整理
最近在研究ExtJS,发现CSDN中有个博客中一系列文档非常优秀,但是没有对目录进行整理,在此稍作整理,也为以后自己研究打下一个基础: 原文作者:低调的草原狼 目录: 1.ExtJS4.2学习 ...
- MyEclipse 安装JRebel进行热部署
安装环境 版本:myeclipse2015stable2 说明:下面是我已经安装了界面 安装过程 进入市场 出现下面提示,不用管它,点Continue 用关键词搜索 配置 进入JRebel配置中心,配 ...
- [剑指OFFER] 斐波那契数列- 跳台阶 变态跳台阶 矩形覆盖
跳台阶 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. class Solution { public: int jumpFloor(int number) ...