034 Search for a Range 搜索范围
给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置。
你的算法时间复杂度必须是 O(log n) 级别。
如果在数组中找不到目标,返回 [-1, -1]。
例如:
给出 [5, 7, 7, 8, 8, 10] 和目标值 8,
返回 [3, 4]。
详见:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
Java实现:
class Solution {
public int[] searchRange(int[] nums, int target) {
int n=nums.length;
int[] result = { -1, -1 };
if(n==0||nums==null){
return result;
}
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = (left + right)>>1;
if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
int index = mid;
while (index >= 0 && nums[index] == target) {
--index;
}
result[0]=index+1;
index = mid;
while (index < nums.length && nums[index] == target) {
++index;
}
result[1]=index-1;
break;
}
}
return result;
}
}
C++实现:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
//时间复杂度为O(logn)
int n = nums.size();
int l = 0, h = n-1, m;
int start = -1, end = -1;
while (l<=h)
{
m = l + ((h - l) >> 1);
if (nums[m]>target)
{
h = m-1;
}
else if (nums[m]<target)
{
l = m + 1;
}
else
{//找到以后,需要确定前后边界
int index = m;
while (index >= 0 && nums[index] == target)
{
index--;
}
start = index + 1;
index = m;
while (index<n&&nums[index] == target)
{
index++;
}
end = index - 1;
break;
}
}
return vector<int>{start,end};
}
};
034 Search for a Range 搜索范围的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- 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 034 Search for a Range
题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...
- Java for LeetCode 034 Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [OJ] Search for a Range
LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
随机推荐
- 一个Web结合Mybatis项目
需要引入apache.commons.dbcp-1.2.2.osgi.jar以及org.apache.commons.pool-1.5.3.jar用来提供JDBC的访问: 需要org.springfr ...
- bzoj 2716 [Violet 3]天使玩偶——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2716 第三道KDtree!仍旧是模板.还有CDQ分治做法,见下面. 数组迷之开大?(开6e5 ...
- 批量清除过期的binlog释放磁盘空间
方案,总共24台db,一台台进去清理肯定不行,得需要写一个脚本,进行批量操作,方案思路大概如下 1, 建立双master列表masterlist; 一个master一行. 2,远程获取master ...
- DSP基础
CCS V5的使用 CCS安装与设置
- js---数组习题---
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaScript设计模式--桥梁模式--引入
1.使用情况 (1)事件的监控 #1,利用页面的button来选择宠物的例子(思路) button.addEvent(element,"click",getPetByBame); ...
- 分页sql汇总
1.oracle数据库分页 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>= ...
- JAVA基础知识总结1(概述)
JAVA概述: 1991 年Sun公司的James Gosling等人开始开发名称为 Oak 的语言,希望用于控制嵌入在有线电视交换盒.PDA等的微处理器. 1994年将Oak语言更名为Java. J ...
- 【总结整理】JQuery小技巧
var item=$("#content").find(".item");//效率最高 var item=$("#content .item" ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint2-摄像头子系统开发\第2节-V4L2图像编程接口深度学习
参考资料: http://www.cnblogs.com/emouse/archive/2013/03/04/2943243.htmlhttp://blog.csdn.net/eastmoon5021 ...