【Search for a Range】cpp
题目:
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的更多相关文章
- leetcode 【 Search for a Range 】python 实现
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 【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 ...
- 【String to Integer (atoi) 】cpp
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Hdu 4738【求无向图的桥】.cpp
题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...
- 【Merge K Sorted Lists】cpp
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 【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 ...
- 【Largest Rectangle in Histogram】cpp
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- 【Binary Tree Inorder Traversal】cpp
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- 【Binary Tree Preorder Traversal】cpp
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
随机推荐
- iOS 获取当前城市
1.倒入头文件 #import <CoreLocation/CoreLocation.h> 2.实现定位协议CLLocationManagerDelegate 3.定义定位属性 @pro ...
- POJ C++程序设计 编程作业—类和对象 编程题#1
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序输出的结 ...
- 软件工程 speedsnail 冲刺5
2015-5-9 完成任务:学习了黑马android教学视频10\11\12集,填写游戏人的姓名功能为明天的记分板准备: 遇到问题: 问题1 Suspicious method call; shoul ...
- Linux下Mysql主从复制(Master-Slave)与读写分离(Amoeba)实践
一.为什么要做Mysql的主从复制(读写分离)?通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低.为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻 ...
- Maven的HTTP代理设置
http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html 第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...
- Thinkphp 下面执行crond
thinkphp开启cli支持 1.tp正好支持cli命令模式,手册的路径为13.7.4 如果是用的其他框架不支持cli,那么只能直接写程序了,其实就是写面向过程的最基础的php代码. 2.在入口文 ...
- devexpress 控制面板汉化方式 参考信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- SIMATIC PCS 7 结构图
- 十天学会单片机Day2键盘检测(独立键盘、矩阵键盘)
1.键盘的分类 编码键盘:键盘上闭合键的识别由专用的硬件编码器实现,并产生键编码号或键值的称为编码键盘,如计算机键盘 非编码键盘:靠软件编程来识别的称为非编码键盘.独立键盘.矩阵键盘 2.按键消抖 ...
- static in C/C++
最近经常碰到static,之前也使用过,但都是一知半解,所以下决心做个整理总结,搞搞灵清它到底用哪些作用. 一.static in C 1.默认初始化为0: 如果不显式地对静态变量进行初始化,它们将被 ...