LeetCode OJ--Search for a Range
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的更多相关文章
- [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 ...
- [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 搜索一个范围(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 ...
- 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 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 034 Search for a Range
题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode 【 Search for a Range 】python 实现
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
随机推荐
- codis 配置
#修改dashboard.toml: coordinator_name = "zookeeper" coordinator_addr = "192.168.56.101: ...
- PHP实现同array_column一样的功能
不用PHP自带的array_column函数实现同样的功能 <?php /** * Created by PhpStorm. * User: 123456 * Date: 2018/9/25 * ...
- python-matplotlib-lec0
直奔主题吧..以下是对matplotlib画图的简单讲解,代码已测试. win7 + pycharm + python 2.7 参考文档: http://old.sebug.net/paper/boo ...
- 数学基础:HUD1124-Factorial(N!末尾0的个数)
Factorial Problem Description The most important part of a GSM network is so called Base Transceiver ...
- luogu1725 琪露诺
单调队列 #include <iostream> #include <cstdio> using namespace std; int n, l, r, dp[400005], ...
- luogu1208 尼克的任务
倒着推就是了 #include <iostream> #include <cstdio> #include <vector> using namespace std ...
- 数据库学习网站和linux学习网站
Oracle ITPub论坛 http://www.itpub.net 著名IT技术论坛.尤以数据库技术闻名. ITPUB论坛的前身应该是建立在 smiling 的 oracle小组,他们搬家前的主页 ...
- 聊聊、Java 网络编程
Socket 编程大家都不陌生,Java 学习中必学的部分,也是 Java网络编程核心内容之一.Java 网络编程又包括 TCP.UDP,URL 等模块.TCP 对应 Socket模块,UDP 对应 ...
- linux基础(Vi编辑器)
整理的linux vi编辑器命令 Vi编辑器,进入方式,输入vi file即可进入编辑模式 1.vi模式(Linux严格区分大小写) Vi所学到的几种模式 模式 主要用途 相应操作 对应命令 普通模式 ...
- rabbitmq exchange type
This is the fourth installment to the series: RabbitMQ for Windows. In thelast installment, we revi ...