题目:

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

代码:

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int pos = Solution::findPos(nums, target, , nums.size()-);
if ( pos==- )
{
ret.push_back(-);
ret.push_back(-);
return ret;
}
int l = Solution::findLeft(nums, target, , pos);
ret.push_back(l);
int r = Solution::findRight(nums, target, pos+, nums.size()-);
ret.push_back(r);
return ret;
}
static int findPos(vector<int>& nums, int target, int begin, int end)
{
if ( begin>end ) return -;
int mid = (begin+end)/;
if ( nums[mid]==target ) return mid;
if ( nums[mid]>target )
{
return Solution::findPos(nums, target,begin,mid-);
}
else
{
return Solution::findPos(nums, target, mid+, end);
}
}
static int findLeft(vector<int>& nums, int target, int begin, int end)
{
if ( begin>end ) return begin;
int mid = (begin+end)/;
if ( nums[mid]<target )
{
return Solution::findLeft(nums, target, mid+, end);
}
else
{
return Solution::findLeft(nums, target, begin, mid-);
}
}
static int findRight(vector<int>& nums, int target, int begin, int end)
{
if ( begin>end ) return end;
int mid = (begin+end)/;
if ( nums[mid]>target )
{
return Solution::findRight(nums, target, begin, mid-);
}
else
{
return Solution::findRight(nums, target, mid+, end);
}
}
};

tips:

按照常规的思路写的。

1. 首先二分查找target变量的某个位置,如果没有则直接返回-1

2. 确定有target变量了,则分左右两边找

  1)左侧:找最左边的target的元素

  2)右侧:找最右边的target元素

注意处理好边界的case。

=================================

学习了一种STL的写法 代码如下:

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
const int l = std::distance(nums.begin(), std::lower_bound(nums.begin(), nums.end(), target));
const int u = std::distance(nums.begin(), std::upper_bound(nums.begin(), nums.end(), target));if (nums[l]!=target)
{
ret.push_back(-);
ret.push_back(-);
}
else
{
ret.push_back(l);
ret.push_back(u>?u-:);
}
return ret;
}
};

非常简洁。

不知道为什么,在mac的sublime上coding时,prev() next() 这俩函数一直不让用。

=============================================

第二次过这道题,就是用二分查找的思路。找左边界,右边界。代码比第一次过的时候简洁一些。

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int l = -;
int r = -;
int begin = ;
int end = nums.size()-;
// search for left bound
while ( begin<=end )
{
int mid = (begin+end)/;
if ( nums[mid]==target )
{
l = mid;
end = mid-;
}
else if ( nums[mid]>target )
{
end = mid-;
}
else
{
begin = mid+;
}
}
if ( l==- ) { ret.push_back(l); ret.push_back(r); return ret; }
// search for right bound
begin = l;
end = nums.size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( nums[mid]==target )
{
r = mid;
begin = mid+;
}
else
{
end = mid-;
}
}
ret.push_back(l);
ret.push_back(r);
return ret;
}
};

【Search for a Range】cpp的更多相关文章

  1. leetcode 【 Search for a Range 】python 实现

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  2. 【Search a 2D Matrix】cpp

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  3. 【String to Integer (atoi) 】cpp

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  4. Hdu 4738【求无向图的桥】.cpp

    题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...

  5. 【Merge K Sorted Lists】cpp

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  6. 【Merge Two Sorted Lists】cpp

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  7. 【Largest Rectangle in Histogram】cpp

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  8. 【Binary Tree Inorder Traversal】cpp

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  9. 【Binary Tree Preorder Traversal】cpp

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...

随机推荐

  1. MVC开发Markdown编辑器(1)

    MVC markdown MVC开发Markdown编辑器(1) 前言 安装 解析 结束语 前言 想在近段时间通过mvc开发个人博客,编辑器希望是markdown风格的,这样写文字会很方便.首先先解决 ...

  2. Web前端年后跳槽必看的各种面试题

    幸运且光荣的被老大安排了一个任务 - “去整理些前端面试题”.年前确实不是招人的好时候,所以我们前端团队经过了超负荷的运转,终于坚持过了春节.春节以后就开始招人啦,这套题考察的目标就是基础基础再基础, ...

  3. C#winform导出数据到Excel的类

    /// <summary> /// 构造函数 /// </summary> public ExportData() { } /// <summary> /// 保存 ...

  4. Android环境配置Sencha Touch

    转自http://www.phonegap100.com/portal.php?mod=view&aid=19 作为你开发的一部分,为安卓设备开发的 Sencha Touch框架应该在安卓虚拟 ...

  5. 在Linux 5/6上使用UDEV SCSI规则配置ASM DISK

    格式化磁盘(略) 识别磁盘(/sbin/scsi_id)  Oracle Linux 5用如下脚本: #!/bin/sh for i in b c d e f g do echo "KERN ...

  6. 检测openOffice关闭 自动重启

    参考http://my.oschina.net/UpBoy/blog/301193   bat解释:循环检测任务列表找到soffice.exe,没找到就调用run.bat   @echo off :l ...

  7. 第五节:AppDomain FirstChance异常通知

    每个AppDomain都可关联一组回调方法:CLR开始查找AppDomain中的catch块时,这些回调方法就会得到调用.这些方法可执行日志记录操作.除此之外,宿主可利用这个机制监视AppDomain ...

  8. 第十四章 调试及安全性(In .net4.5) 之 对称及非对称加密

    1. 概述 本章内容包括:对称及非对称加密算法..net中的加密类.使用哈希操作.创建和管理签名认证.代码访问权限 和 加密字符串. 2. 主要内容 2.1 使用对称和非对称加密 ① 对称加密:使用同 ...

  9. Effective C# 学习笔记(原则二:为你的常量选择readonly而不是const)

    原则二.为你的常量选择readonly而不是const      Prefer readonly to const 对于常量,C#里面有两个不同的版本:运行时常量(readonly)和编译时常量(co ...

  10. SQL中的内连接外连接和交叉连接是什么意思?

    内连接又分为等值连接.自然连接和不等连接三种. 外连接分为左外连接(LEFT OUTER JOIN或LEFT JOIN).右外连接(RIGHT OUTER JOIN或RIGHT JOIN)和全外连接( ...