leetcode - 34. Search for a Range - Medium

descrition

Given an array of integers sorted in ascending order, 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].

解析

对于有序数组的查找问题,基本上都可以使用折半查找的思路。再者题目的要求复杂度是 O(log n),因此我们需要两次折半查找。如果我们只用一次折半查找,找到数组 target 出现的任意一个位置,然后线性遍历找到最左边和最右,需要的复杂度是 O(n)。

具体实现代码如下。

code


#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ans(2, -1);
int ileft = binarySearchLeftMost(nums, target);
if(ileft < 0)
return ans; int iright = binarySearchRightMost(nums, target); ans[0] = ileft;
ans[1] = iright; return ans;
} int binarySearchLeftMost(vector<int>& nums, int target){
int ans = -1; // save the left most index in nums which equal to target
int ileft = 0, iright = nums.size() - 1; while(ileft <= iright){
int imid = ileft + (iright - ileft) / 2;
if(nums[imid] == target){
ans = imid;
iright = imid - 1;
}else if (nums[imid] < target){
ileft = imid + 1;
}else{
// nums[imid] > target
iright = imid - 1;
}
} return ans;
} int binarySearchRightMost(vector<int>& nums, int target){
int ans = -1;
int ileft = 0, iright = nums.size() - 1; while(ileft <= iright){
int imid = ileft + (iright - ileft) / 2;
if(nums[imid] == target){
ans = imid;
ileft = imid + 1;
}else if (nums[imid] < target){
ileft = imid + 1;
}else {
// nums[imid] > target
iright = imid - 1;
}
} return ans;
} }; int main()
{
return 0;
}

[array] leetcode - 34. Search for a Range - Medium的更多相关文章

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

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

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

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

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

  5. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

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

  7. [leetcode 34] search for a range

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

  8. Java [leetcode 34]Search for a Range

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

  9. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

随机推荐

  1. 《疯狂java讲义》读后感

    <疯狂java讲义·第三版>,全书共851页,18章. 目录如下: 第1章 Java语言概述与开发环境 第2章 理解面向对象 第3章 数据类型和运算符 第4章 流程控制与数组 第5章 面向 ...

  2. 【Java疑难杂症】有return的情况下try catch finally的执行顺序

    有这样一个问题,异常处理大家应该都不陌生,类似如下代码: public class Test { public static void main(String[] args) { int d1 = 0 ...

  3. C语言之计算log2

    #include<stdio.h>int main(){int num,count=0,i=0,ret=0;scanf("%d",&num);count=num ...

  4. robotframework自动化系统:操作mysql数据库

    随着项目自动化深入和不断完善,大部分功能都已经能完成了自动化的操作:但是在设备添加的时候,遇到了难题.添加设备的时候mac必须是服务器设备管理中已经存在的mac地址,且是没有关联或绑定用户的设备信息. ...

  5. Redis-Nosql数据库入门

    简介 Redis是Nosql数据库的一种,可基于内存亦可持久化的日志型.是一个Key-Value数据库,多用在缓存方面 安装 Windows 下载地址, 最新版本的Redis好像仅支持64位 Wind ...

  6. 【微服务】之四:轻松搞定SpringCloud微服务-负载均衡Ribbon

    对于任何一个高可用高负载的系统来说,负载均衡是一个必不可少的名称.在大型分布式计算体系中,某个服务在单例的情况下,很难应对各种突发情况.因此,负载均衡是为了让系统在性能出现瓶颈或者其中一些出现状态下可 ...

  7. CCF-201412-2-Z字形扫描

    问题描述 试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zi ...

  8. mysql的explain

    explain 一般用于分析sql.  如下 [SQL] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 2 ...

  9. Nginx日志切割案例讲解,Nginx的知识讲解

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如在产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将会导致日志文件容量 ...

  10. SQL Server学习之路(二):主键和外键

    0.目录 1.定义 1.1 什么是主键和外键 1.2 主键和外键的作用 1.3 主键.外键和索引的区别 2.主键(primary key) 2.1 通过SSMS设置主键 2.2 通过SQL语句设置主键 ...