给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置。
你的算法时间复杂度必须是 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 搜索范围的更多相关文章

  1. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  2. 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 ...

  3. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

  8. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  9. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

随机推荐

  1. encodeURI,encodeURIComponent编码

    encodeURI().encodeURIComponent().decodeURI().decodeURIComponent() URL编码 Global对象的encodeURI()和encodeU ...

  2. Log4Net日志记录介绍

    原文地址 : http://www.cnblogs.com/wolf-sun/p/3347373.html#3009010 简介 log4net库是Apache log4j框架在Microsoft . ...

  3. Python:内置函数zip()

    zip函数接受任意多个可迭代对象作为参数,将对象中对应的元素打包成一个tuple,然后返回一个可迭代的zip对象. 这个可迭代对象可以使用循环的方式列出其元素 若多个可迭代对象的长度不一致,则所返回的 ...

  4. JavaScript权威指南读书笔记【第一章】

    第一章 JavaScript概述 前端三大技能: HTML: 描述网页内容 CSS: 描述网页样式 JavaScript: 描述网页行为 特点:动态.弱类型.适合面向对象和函数式编程的风格 语法源自J ...

  5. javaScript之事件处理程序

    事件就是用户或浏览器自身执行的某个动作,JavaScript与HTML的交互也是通过事件实现的.而相应某个事件的函数就叫做事件处理函数.包括以下几种: 1.HTML事件处理程序    某个元素支持的每 ...

  6. 阶段3-团队合作\项目-网络安全传输系统\sprint0-产品规划与设计\第2课-产品功能模型设计

  7. .NET中的泛型委托

    .Net中有一个内置的委托 Func 它总共有以下5种形式 1.  Func<TResult> 2.  Func<T,TResult> 3.  Func<T1,T2,TR ...

  8. 1.sql注入原理

    一.什么是sql注入呢?         所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露V ...

  9. UVa 11468 Substring (AC自动机+概率DP)

    题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...

  10. xilinx planahead partial reconfiguration

    1.为什么要使用reconfiguration? reconfiguration,即逻辑可重配,指的是FPGA的逻辑只加载指定区域(功能)的逻辑,而对除此之外的逻辑不产生影响,最常用的就是PCIe/P ...