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. 【wqs二分 决策单调性】HHHOJ#261. Brew

    第一道决策单调性…… 题目描述 HHHOJ#261. Brew 题目分析 挺好的……模板题? 寄存了先. #include<bits/stdc++.h> typedef long long ...

  2. LeetCode之Weekly Contest 93

    第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的 ...

  3. ccf 201803-1 跳一跳(Python实现)

    一.原题 问题描述 试题编号: 201803-1 试题名称: 跳一跳 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱. 简化 ...

  4. Virtual Friends HDU - 3172 (并查集+秩+map)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  5. German Collegiate Programming Contest 2018​

    // Coolest Ski Route #include <iostream> #include <cstdio> #include <cstring> #inc ...

  6. 设置ubuntu12.04的dasher-自动隐藏,左上角

    点击右上角的齿轮,--> “system setting”--“Appearance” 在“Look”标签中: Theme:Ambiance Launch icon size :32 选择桌面背 ...

  7. foreach遍历数组的表格

    <?php /** * * @authors Your Name (you@example.org) * @date 2017-03-17 19:06:19 * @version $Id$ */ ...

  8. UVa 10723 LCS变形 Cyborg Genes

    题解转自: UVA 10723 Cyborg Genes - Staginner - 博客园 首先这个题目肯定是按最长公共子序列的形式进行dp的,因为只有保证消去的一部分是最长公共子序列才能保证最后生 ...

  9. Python并发(二)

    并发是指一次处理多件事,而并行是指一次做多件事.二者不同,但互相有联系.打个比方:像Python的多线程,就是并发,因为Python的解释器GIL是线程不安全的,一次只允许执行一个线程的Python字 ...

  10. jenkins 之 Android 打包及上传至蒲公英

    准备条件 iMAC,非必须(如果是 安卓 和 苹果 可以在同一台电脑上打包则要 Mac OS 系统的电脑,如果是只是给安卓打包 windows 电脑也是可以的, window 下 需要把 ls 换成 ...