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 ...
随机推荐
- 解决mysql出现的问题#1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i
最近在学flask, 在访问主页时,一直出现1055错误,在网上找的解决方法是删除ONLY_FULL_GROUP_BY,当时是删除了,但是退出在进行select @@sql_mode时,仍出现ONLY ...
- Java使用ResourceBundle类读取properties文件中文乱码的解决方案
Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO-8859-1编码格式,这对于开发工具默认为UTF-8 ...
- 能力不足之 根据时序图转化为Verilog代码
不能够把时序图看的非常透彻,然后把时序图写成Verilog代码,有时候甚至搞不清楚信号之间的时序关系.
- linux的一些权限的操作 chmod
u ---属主 g ---用户组 o ---其他组 + ----赋予权限 - ----取消权限 = ----赋予权限并取消以前的权限 r = 4 //读 w =2 //写 x =1 //执 ...
- 20181210(os,os.path,subprocess,configparser,shutil)
1.os模块 os表示操作系统,该模块主要处理与操作系统相关的操作.最常用的是文件操作:打开,读取,删除,复制,重命名. 重点掌握增删改查的函数操作. import os# 获取当前执行文件所在文件夹 ...
- python数据类型之集合(set)和其常用方法
集合是一个无序的,不重复的数据组合 作用(集合的重点):1.去重,把一个列表变成集合就自动去重了2.关系测试,测试两组数据库之前的交集.差集.并集等关系 s = {1, 1, 2, 2, 3, 4, ...
- hdu 1011 Starship Troopers(树形背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- BZOJ 4369: [IOI2015]teams分组
把一个人看成二维平面上的一个点,把一个K[i]看成左上角为(0,+max),右下角为(K[i],K[i])的一个矩阵,那么可以很好地描述人对于询问是否合法(我也不知道他怎么想到这东西的) 然后把一组询 ...
- loj2005 「SDOI2017」相关分析
鬼畜线段树--Orz Capella #include <iostream> #include <cstdio> using namespace std; int n, m, ...
- Selenium WebDriver- 操作JavaScript的Alert弹窗
弹层和弹框是有区别的,弹框是那种完全没样式的框子:弹层是可以直接看到html的,有样式 #encoding=utf-8 import unittest import time from seleniu ...