34 Search for a Range(目标数的范围Medium)
题目意思:递增数组,找到目标数的范围,找不到则返回[-1,-1]
思路:折半查找
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int start=,end=nums.size()-;
vector<int> ans;
while(start<=end){
int temp=(start+end)/;
if(nums[temp]>target){
end=temp-;
}
else if(nums[temp]<target){
start=temp+;
}
else if(nums[temp]==target){
int flag1=temp,flag2=temp;
while(flag1>&&nums[flag1-]==target){
--flag1;
}
while(flag2<nums.size()-&&nums[flag2+]==target){
++flag2;
}
ans.push_back(flag1);
ans.push_back(flag2);
return ans;
}
}
ans.push_back(-);
ans.push_back(-);
return ans;
}
};
时间复杂度:O(logn)
运行时间:12ms
代码太冗余了,以后再精简吧
34 Search for a Range(目标数的范围Medium)的更多相关文章
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [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 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- 34. Search for a Range
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- Qt入门(7)——窗口几何结构
QWidget提供了几个处理窗口几何结构的函数.这些函数中的几个操作纯客户区域(例如不包含窗口框架的窗口),其它一些包括窗口框架.它们之间的区别在某种意义上被完成覆盖明显地最普通的方法.包括窗口的框架 ...
- github上排名靠前的java项目之_storm
1.和hadoop的比较 Storm: 分布式实时计算,强调实时性,常用于实时性要求较高的地方 Hadoop:分布式批处理计算,强调批处理,常用于数据挖掘.分析 2.Hadoop是实现了mapre ...
- 线段树(维护最大值):HDU Billboard
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDOJ 2081 手机短号
Problem Description 大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号.假设所有的短号都是是 6+手机号的后5位,比 ...
- 在Ubuntu中安装Redis
原文地址:http://blog.fens.me/linux-redis-install/ 在Ubuntu中安装Redis R利剑NoSQL系列文章,主要介绍通过R语言连接使用nosql数据库.涉及的 ...
- UVA 11478 Halum(差分约束)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...
- SRM 403(1-250pt, 1-500pt)
DIV1 250pt 题意:称各个数位只含有4和7的数为lucky number,给定a,b,求[a, b]中的lucky number有多少个.a, b <= 10^9 解法:很明显的数位dp ...
- pygame学习资料
pygame下载地址: https://bitbucket.org/pygame/pygame/downloads 12岁的少年教你用Python做小游戏 Beginning Game Program ...
- Web项目初始化过程
在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>. 接着容器会创建一个ServletCont ...
- idea unknow facet type web 解决方案
菜单 -->Preferences-->Plugins 添加tomcat支持 如图: 然后 项目project-setting中 可以添加 web类型的facets了 pasting