leetcode116:search-for-a-range
题目描述
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].
输出
[3,4]
class Solution {
public:
/**
*
* @param A int整型一维数组
* @param n int A数组长度
* @param target int整型
* @return int整型vector
*/
vector<int> searchRange(int* A, int n, int target) {
// write code here
vector< int> res(2,-1);
if (A==nullptr || n<=0)
return res;
int low=lower_bound(A, A+n, target)-A;
if (low==n || A[low]!=target)
return res;
else res[0]=low;
int high=upper_bound(A, A+n,target)-A-1;
res[1]=high;
return res;
}
};
class Solution {
public:
/**
*
* @param A int整型一维数组
* @param n int A数组长度
* @param target int整型
* @return int整型vector
*/
vector<int> searchRange(int* A, int n, int target) {
// write code here
vector<int> res(2,-1);
if (A==nullptr || n<=0)
return res;
int low=0,high=n-1;
while (low<=high)
{
int middle =(high+low)>>1;
if (A[middle]<target)
low=middle+1;
else
high=middle-1;
}
int low2=0,high2=n-1;
while (low2<=high2)
{
int middle2=(high2+low2)>>1;
if (A[middle2]<=target)
low2=middle2+1;
else
high2=middle2-1;
}
if (low<=high2){
res[0]=low;
res[1]=high2;
}
return res;
}
};
leetcode116:search-for-a-range的更多相关文章
- 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 ...
- 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 ...
- [OJ] Search for a Range
LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- 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::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- [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
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
随机推荐
- P3660 [USACO17FEB]Why Did the Cow Cross the Road III G
Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...
- 用< 100行代码向EPUB或Web服务器添加视频回放
下载source - 32.3 KB 下载latest version from GituHub 介绍 在我 在关于CodeProject的前一篇文章中,我展示了一个简单的EPUB查看器 Androi ...
- TP5隐藏入口文件
1,进入根目录,打开public文件夹,里面有个.htaccess文件 2,将这段代码改成?s= 3,不修改该文件,想要隐藏入口文件则会报错 4,改了文件之后是 5,改了入口文件为了隐藏 .php
- 虚拟主机和ECS的选择——有的坑你可以不躺,有的钱你可以不花(一)
一直想做网站,由于最开始虚拟主机有优惠,所以三年前买了虚拟主机,后来一直续费,间歇性使用过,发现很多功能都不行. 昨天准备买新的,然后想起学生购买有优惠,于是开始了学生认证之旅. 首先,看一下之前 ...
- centos8下启用rc-local服务
一,centos8不建议写rc.local,默认启动时执行的命令放到何处? 以前我们会把linux开机执行的命令写入到/etc/rc.local 在centos8上系统不再建议我们写入到rc.loca ...
- laravel重写
laravel location / { try_files $uri $uri/ /index.php?$query_string; } ci location / { try_files $uri ...
- 本地ssh快速登录 ssh免密登录
每次登录都要ssh -p wang@xx.xx.xx.xx 虽然做了公钥验证 https://www.cnblogs.com/php-linux/p/10795913.html 不需要输入密码,但是每 ...
- tamcat7.0(安装文件下载)
安装包:http://files.cnblogs.com/files/chenghu/apache-tomcat-7.0.27.rar http://files.cnblogs.com/files/c ...
- Linux操作系统的介绍和安装教程(Centos6.4)
路漫漫其修远兮,吾将上下而求 Linux的简单介绍 Linux最初是由芬兰赫尔辛基大学学生Linus Torvalds开发的,由于自己不满意教学中使用的MINIX操作系统, 所以在1990年底由于个人 ...
- 人生第一篇博客 , 当然是经典的 "Hello World"
package com.cqvie.Hello; public class FirstDemo { public static void main(String[] args) { System. ...