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 ...
随机推荐
- C++:100阶乘数组输出
#include <iostream> using namespace std; int main(){ int i =1; int a[2048]={0}; while(i !=101) ...
- centos7重启后/etc/resolv.conf 被还原解决办法
每次重启服务器后,/etc/resolv.conf文件就被自动还原了,最后发现是被Network Manager修改了. 查看Network Manager服务状态 systemctl status ...
- python入门:求1-2+3-4+5...99的所有数的和(自写)
#!/usr/bin/env pyhton # -*- coding:utf-8 -*- #求1-2+3-4+5...99的所有数的和(自写) """ 给x赋值为0,给y ...
- 如何用纯 CSS 创作一颗逼真的土星
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/EpbaQX 可交互视频 ...
- matplotlib学习记录 七
# 绘制直方图 # 假设你获取了250部电影的时长(列表a中),希望统计出这些电影时长的分布状态(比如时长为100分钟到 # 120分钟电影的数量,出现的频率)等信息,你应该如何呈现这些数据? fro ...
- scanf(),gets(),getchar()
scanf()与gets()区别: scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用gets() ...
- 思维水题:UVa512-Spreadsheet Tracking
Spreadsheet Tracking Data in spreadsheets are stored in cells, which are organized in rows (r) and c ...
- B - CD UVA - 624
https://cn.vjudge.net/contest/224070#problem/B #include <iostream> #include <cstring> #i ...
- 原生Ajax+springBoot实现用户登录
思路:用户输入登录信息——信息传到后台——数据库查询——比较查询结果——返回登录信息(成功/失败) html页面代码: <!DOCTYPE html> <html lang=&quo ...
- Angular Vue React 框架中的 CSS
框架中的 CSS Angular Vue React 三大框架 Angular Vue 内置样式集成 React 一些业界实践 Angular Angular . js (1.x):没有样式集成能力 ...