leetcode 34 Search for a Range(二分法)
Search for a Range
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].
题解:很简单,lower_bound()和upper_bound()两个函数用一下就好。
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int>ans;
int n=nums.size();
int a=lower_bound(nums.begin(),nums.end(),target)-nums.begin();
if((a==n)||(nums[a]!=target)){
ans.push_back(-);
ans.push_back(-);
return ans;
}
else{
int b=upper_bound(nums.begin(),nums.end(),target)-nums.begin();
ans.push_back(a);
ans.push_back(b-);
return ans;
}
}
};
leetcode 34 Search for a Range(二分法)的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- 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 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
随机推荐
- linux的文件夹权限
r:读取文件夹结构清单的权限,可以列出该文件夹下的所有文件. w:更改目录结构清单的能力,可以新建文件和目录,删除文件和目录(不管这个文件是否属于你),对文件和目录更名,移动文件和目录. x:具有x权 ...
- 在Linux中显示日历(cal)
cal 2013 显示2013年整年日历 cal 7 2013 显示2013年 7 月 日历
- WebApi 中使用 Token
1.登陆的时候根据用户信息生成Token var token = FormsAuthentication.Encrypt( new FormsAuthenticationTicket( , " ...
- Storm编程模型及Worker通信机制
1.编程模型 2.Worker通信机制
- 解决Eclipse的Team菜单中没有SVN选项的问题
我们想使用SVN向SVN服务器上传代码,但Eclipse默认情况下却没有SVN选项,如下图所示. 默认只有GIT,如下图所示. 那么,我们怎么解决这个问题呢? 第一步:如下图所示. 第二步:在&quo ...
- SpringMVC的第一个入门案例
用户提交一个请求,服务器端处理器在接收到这个请求后,给出一条欢迎信息,在页面中显示. 第一步:导入jar包 在原有Springjar包基础上添加2个jar包 spring-webmvc-4.2.0.R ...
- The given 'driver' ] is unknown, Doctrine currently supports only the follo wing drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, oci8, ibm_db2, pdo
[Doctrine\DBAL\DBALException] The given 'driver' ] ...
- 如何将cordova导入Android studio,只需两步即可
Cordova的技术交流新群 微信公众号:
- deviceToken的获取(一)
1.获得deviceToken的过程 1>客户端向苹果服务APNS,发送设备的UDID和英语的Bundle Identifier.2>经苹果服务器加密生成一个deviceToken ...
- python cookbook 字符串和文本
使用多个界定符分隔字符串 import re line = 'asdf fjdk; afed, fjek,asdf, foo' print(re.split(r'[;,\s]\s*', line)) ...