题目意思:递增数组,找到目标数的范围,找不到则返回[-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)的更多相关文章

  1. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  2. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  3. [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 ...

  4. 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 ...

  5. 【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 ...

  6. 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 ...

  7. 34. Search for a Range

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  8. 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 ...

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)

    Problem Description As the increase of population, the living space for people is becoming smaller a ...

  2. C/C++常用算法【C语言顺序查找(随机数)】【1】

    这是我学习唐峻,李淳的<C/C++常用算法第一天> 1.8.1. 查找数字: 程序随机生成一个拥有20个整数数据的数组,然后输入要查找的数据.接着,可以采用醉简单的逐个对比的方法进行查找, ...

  3. json格式键盘编码对照表

    整理了一份JSON格式的键盘编码对照表.欢迎转载,但请注明出处,谢谢! { VK_BACK: 8, //退格键 VK_TAB: 9, //TAB键 VK_RETURN: 13, //回车键 VK_SH ...

  4. 每一个web开发者都应该了解的HTTP/2

    我认为每一个 web 开发者都应该对这个支撑了整个 Web 世界的 HTTP 协议有所了解,这样才能帮助你更好的完成开发任务.在这篇文章中,我将讨论什么是 HTTP,它是怎么产生的,它的地位,以及我们 ...

  5. 安装Wamp后 Apache无法启动的解决方法

    安装Wamp后 Apache无法启动的解决方法,网上的解决方案可以说是五花八门,有些说了一大推,一点作用都起不到. 其实解决方法只需两步: 1.安装路径不能包含有中文,这个我不知道为什么,总之如果安装 ...

  6. 【protobuf进阶】读取proto实体里的extensionObject对象的方法

    //设置扩展对象 ProtoBuf.Extensible.AppendValue //读取扩展对象 ProtoBuf.Extensible.GetValue 最近通过C#的TcpClient调用jav ...

  7. Android 网络框架--Retrofit

    1.导入Jar包 compile 'com.google.code.gson:gson:2.8.0' compile 'com.squareup.retrofit2:retrofit:2.1.0' c ...

  8. tomcat URL简写案例:模拟站点www.baidu.com的訪问

    tomcat URL简写案例:模拟站点  * 实际URL:http://www.baidu.com:8080/myweb/1.html  * 实际位置:F:\mywebapps\myweb\1.htm ...

  9. 妹子图太多怎么看才好,Swing来支招

    近期事少,翻开非常久曾经写的小程序,创意倒是尚可,代码写的却比較基础,非常多东西没有实现,略改了改形成了如今的模样,如今大家都忙着大数据,中间件,web开发,偶尔看看Java Swing的作品,也许能 ...

  10. iOS中sqlite3操作

    声明:下面命令我没有所有使用过, 仅用于收藏, 欢迎大家指出当中的错误 'SELECT  count(*)   FROM sqlite_master WHERE type="table&qu ...