题目:

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].  (Medium)

分析:

标准的二分搜索题,找一个target的范围,也就是first position 元素等于target 和last position元素等于。

写两次二分搜索,注意中间start,end的变化情况的不同,一个为了保留住第一个满足条件的,一个为了保留住最后一个满足条件的。

代码:

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result;
if (nums.size() == ) {
return result;
}
int start = , end = nums.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (nums[mid] < target) {
start = mid;
}
else {
end = mid;
}
}
if (nums[start] == target) {
result.push_back(start);
}
else if (nums[end] == target) {
result.push_back(end);
} start = ;
end = nums.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (nums[mid] <= target) {
start = mid;
}
else {
end = mid;
}
}
if (nums[end] == target) {
result.push_back(end);
}
else if (nums[start] == target) {
result.push_back(start);
}
if (result.size() != ) {
return result;
}
else {
return vector<int> {-, -};
}
}
};
 

LeetCode34 Search for a Range的更多相关文章

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

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

  3. [OJ] Search for a Range

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

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

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

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

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

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

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

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

  8. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  9. 【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 ...

随机推荐

  1. windows下执行build_native.sh报权限问题

    编辑build_native.sh 在前面加上 chmod 777 -R ./* 

  2. 转】MyEclipse使用总结——MyEclipse10安装SVN插件

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3497016.html 感谢! 一.下载SVN插件subclipse 下载地址:http://subclipse. ...

  3. MYSQL数据库性能调优之八:mysql日志

    MySQL日志 主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志.中继日志: 使用 SHOW GLOBAL VARIABLES LIKE '%log%';  查询所有日志配置详情: 一. ...

  4. Dynamic Method Resolution

    [Dynamic Method Resolution] @dynamic directive 用于声明属性的方法dynamic loading,which tells the compiler tha ...

  5. IOC知识

    1.两个基本概念 IOC(Inversion of Control ):反转控制,即将控制权反转出去. DI(Dependency Injection):依赖注入,根据依赖关系进行注入. DI是实现I ...

  6. Median of Two Sorted Arrays-----LeetCode

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  7. struts2 集成 easyui

    关键点: json数据格式 获取json数据 输出json 分页 #json数据格式# datagrid: {"total":1,"rows":[{" ...

  8. <filter-mapping> 的 <dispatcher> 的作用

    The dispatcher has four legal values: FORWARD, REQUEST, INCLUDE, and ERROR. A value of FORWARD means ...

  9. RS232串口用事件接受数据(一问一答)

    private void button1_Click(object sender, EventArgs e) { serialPort1.Open(); serialPort1.DataReceive ...

  10. Eclipse 和 NetBeans 快捷键即其他常用功能比较

    按: 自己用 Eclipse, 常用的也就这些功能, 在用 NetBeans 时, 有些不顺手, 因此列表如下. Eclipse和NetBeans常用快捷键对比:  功能  Eclipse     N ...