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(二分法)的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  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 (找到一个范围)

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

  5. Java [leetcode 34]Search for a Range

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

  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 (有序数组中查找给定数字的起止下标)

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

  8. [leetcode 34] search for a range

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

  9. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

随机推荐

  1. python#模拟发送电子邮件

    #-*- coding:utf-8 -*- #模拟发送电子邮件 from email.mime.text import MIMEText from_addr = 'aa@sss.com' passwo ...

  2. Django开发之html交互

    html中用户输入信息,由Django的view.py处理,大致用到了以下几类格式: 1. 文本框 <input type="text" name="vid&quo ...

  3. 【hiho一下】第一周 最长回文子串

    题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...

  4. Manager模块 队列 管道 进程池

    Manager模块 作用:  多进程共享变量. Manager的字典类型: 如果value是简单类型,比如int,可以直接赋值给共享变量,并可以后续直接修改 如果value是复杂类型 ,比如list, ...

  5. ubuntu16.04下hive安装与配置

    Hive是什么? 由Facebook开源用于解决海量 结构化日志的数据统计: Hive是基于Hadoop的一个 数据仓库工具,可以将结构化的数据文件映射 成一张表,并提供类SQL查询功能: 构建在Ha ...

  6. 关于matlab曲线拟合的问题

    matlab 曲线拟合工具箱,app->curve fitting 可以使用generate直接产生代码,生成的是函数 该函数直接返回的结果为cfit格式,直接读取不了,网上有网友说可以采用y ...

  7. 【C语言】Linux C调用系统命令

    最近研究深度学习,做视频分析和检测,用到C语言,以前都是写python的,不过没关系,计算机语言都是相通的,差不多原理是一样的,只是语法不太一样. 下面介绍linux C语言种调用本地命令,访问一个地 ...

  8. Python socket server demo

    #coding:utf-8 from socket import * #开启ip和端口 ip_port = ("192.168.1.103",8088) print ip_port ...

  9. 小程序getApp() 被删除坑

    在一个非page的js文件内使用getApp,当前台切到后台的时候,定义的var app = getApp()被删除了 如:新建一个app-libs.js start: function() { va ...

  10. Android SDK上手指南1:应用程序结构

    一直说要学java要学android开发,可是一直胡乱地忙活这忙活那,之前开始学了一点也中断了.说是没时间,都是借口,回顾一下自己的生活感觉缺少点激情,没有什么奋斗的动力,所以好多时间就浪费了.刚刚考 ...