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 ...
随机推荐
- pyinstall 使用笔记
1.下载pyinstaller 目前pyinstaller支持的python版本为2.3-2.7,可以到http://www.pyinstaller.org/官网下载. 2.安装 下载完成后,解压即可 ...
- Jquery LigerUI框架学习(二)之Tree于Tab标签实现iframe功能
LigerUI框架Tree于Tab标签动态使用,当点击Tree后动态创建Tab标签,和通常用的iframe框架功能类似 Tree中的关键代码 //Tree初始化 $("#tree1" ...
- WPF 界面控件遍历和后台行为绑定写法
InvokeCommandAction ic = new InvokeCommandAction(); ic.Command = tree.SelectedItemCommand;//绑定的命令 ic ...
- Zend Studio 12.0.2正式版发布和破解方法,zend studio 12.0.1汉化,相式设置为Dreamweaver,空格缩进为4个, 代码默认不折叠的设置,Outline中使用的图形标志,代码颜色之eot设置。
背景:zend studio 12.0.2 修复了一个12.0.1的: Fixed problem with referenced variables marked as undefined,我都说 ...
- C++对MS SQL Server的操作
今天因为在做一份C++的期末作业,突然想用C++来链接数据库,实现数据的重复利用,所以就作死去百度搜了一下. 更巧的事情是,一搜居然还有很多搜索结果,然后就照着做了. 做的过程很艰辛,就不一一诉说了, ...
- 使用Putty连接VirtualBox的Ubuntu
从vbox中安装了ubuntu server,然后用ssh连过去,发现有一个错误:server unexpectedly closed network connection.猛然发现,ssh没有安装. ...
- Leetcode#78 Subsets
原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...
- POJ 1068 AC 2014-01-07 15:24 146人阅读 评论(0) 收藏
POJ的题目都是英文的,所以,,,还是直接贴代码吧 #include<stdio.h> int main(){ int x,y,z; int n,nm,max; scanf("% ...
- ssh-add 报错 Could not open a connection to your authentication agent
ERROR: [root@testcentos01 ~]# ssh-add Could not open a connection to your authentication agent 在shel ...
- CentOS安装视频播放器SMPlayer
首先下载rpmforg,下载对应的版本,就是对应CentOS版本,还有32位与64位也要对应上.地址如下: http://wiki.centos.org/AdditionalResources/Rep ...