http://oj.leetcode.com/problems/search-for-a-range/

要求复杂度为O(lgn),用二分查找的思想。

#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
void fun(int* A,int start,int end,int target,vector<int> &ans)
{
if(start>end || ( start == end && A[start]!= target ))
{
ans.push_back(-);
ans.push_back(-);
return;
}
if(start == end && A[start] == target)//只有一个元素了
{
ans.push_back(start);
return;
}
if(A[start] == target && target ==A[end])
{
ans.push_back(start);
ans.push_back(end);
return;
}
int middle = (start + end)/;
if(A[middle]<target)
fun(A,middle+,end,target,ans);
else if(A[middle]>target)
fun(A,start,middle-,target,ans);
else
{
ans.push_back(middle);
return;
}
return;
}
vector<int> searchRange(int A[], int n, int target) {
vector<int> ans;
if(n == )
return ans; fun(A,,n-,target,ans); int small,large;
if(ans[] == -) //肯定没找到
{
ans.clear();
ans.push_back(-);
ans.push_back(-);
return ans;
}
if(ans.size()==) //ans中只有一个元素
small = large = ans[]; if(ans.size() == )
{
small = ans[];
large = ans[];
}
while(small>= && A[small] == target) //往左边扩展
small--;
while(large<=n- && A[large] == target) //往右边扩展
large++;
if(small == - || A[small]!=target) //考虑上面while循环的退出条件
small++;
if(large== n || A[large]!=target)
large--;
ans.resize(); //可能ans的size是1
ans[] = small; //将扩展后的再赋值回来
ans[] = large;
return ans;
}
}; int main()
{
Solution myS;
int A[] = {,,,,,,,};
vector<int> ans = myS.searchRange(A,,);
return ;
}

LeetCode OJ--Search for a Range的更多相关文章

  1. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

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

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

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

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

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

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

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

  7. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  8. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. leetcode 【 Search for a Range 】python 实现

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

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

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

随机推荐

  1. 【线段树 扫描线 二维数点】loj#6276. 果树

    路径计数转成二维数点很妙啊 题目描述 NiroBC 姐姐是个活泼的少女,她十分喜欢爬树,而她家门口正好有一棵果树,正好满足了她爬树的需求. 这颗果树有 $N$ 个节点,标号 $1 \ldots N$ ...

  2. 科学计算库Numpy——numpy.ndarray

    创建ndarray 元素类型 对于ndarray结构来说,里面所有的元素必须是同一类型的,如果不是的话,会自动的向下进行转换. 元素类型所占字节数 数组维数 元素个数 数组的维度 数组中填充固定值 索 ...

  3. List<Object>删除某一个Object

    1.直接删除: List<LineShop> tlineprices  = new ArrayList<>(); tlineprices.remove(0); 2.使用遍历删除 ...

  4. PAT Basic 1080

    1080 MOOC期终成绩 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分 ...

  5. 如何用ADMINISTRATOR登陆WIN7

    Windows 7系统出于安全考虑,将系统超级管理员帐户(Administrator)隐藏了,不允许"普通用户"使用.很多时候特别是安装一些应用软件时,由于兼容的问题,普通权限的用 ...

  6. LAMP动态网站安装脚本

    #!/bin/bash #auto make install LAMP #by authors zhangjianghua #httpd define path variable H_FILES=ht ...

  7. Activity树图

  8. Java集合数据类型

    Java集合如Map.Set.List等所有集合只能存放引用类型数据,它们都是存放引用类型数据的容器,不能存放如int.long.float.double等基础类型的数据. 1. 集合存储对象 Jav ...

  9. 聊聊、Docker 安装

  10. uiautomator 一个简单脚本创建流程

    http://www.codeceo.com/article/android-ui-auto-test.html