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 ...
随机推荐
- vim学习与理解
1. 转变思维 --> vim 无鼠标文本编辑工具 在鸟哥的linux私房菜中,是这么说明linux的: 1. vim是linux like系统中非常强大的一个文本编辑工具,历史悠久,很多系统都 ...
- 【转】JAVA程序中Float和Double精度丢失问题
原文网址:http://blog.sina.com.cn/s/blog_827d041701017ctm.html 问题提出:12.0f-11.9f=0.10000038,"减不尽" ...
- Codeforces Round #324 (Div. 2)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- Jenkins 学习资料
学习资料: iTech's Blog: Jenkins 入门总结 爱自己: 18篇博客 阳光温暖了心情: 17 篇博客 官网 参考: 构建基于Jenkins + Github的持续集成环境 CI持续集 ...
- VGO新闻 - VGO
VGO新闻 - VGO VGO天津伊势丹店盛装揭幕 VGO天津伊势丹店盛装揭幕2013年9月7日,VGO(微高)全国首家实体店在天津伊势丹百货盛装开幕.现场,100多位商场领导及业内同仁共同出席了
- DTD与XSD的区别
DTD=类型定义(Documnet Type Definition) XSD=XML结构定义 ( XML Schemas Definition ) DTD 就相当于 XML 中的“词汇”和“语法”,不 ...
- bootstrap 仿实例
bootstrap实现一个网页 html文件 <!DOCTYPE html> <html> <head lang="en"> <meta ...
- Android中scrollview的scrollto方法不起作用的办法
有时候,我们在onCreate函数中调用ScrollBy函数.ScrollTo函数,会出现无效果的情况 public class ShowTraffic extends Activity Scroll ...
- [AngularJS] Enable Animations Explicitly For A Performance Boost In AngularJS
http://www.bennadel.com/blog/2935-enable-animations-explicitly-for-a-performance-boost-in-angularjs. ...
- Android源代码分析之Framework的MediaPlayer
在Android中MediaPlayer用来播放音频和视频文件,在这里分析下在Framework层中MediaPlayer是怎样调用的.MediaPlayer的代码位于:./frameworks/ba ...