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 ...
随机推荐
- java实现版本号的比较
之前比较客户端版本号,一直用的是String.compareTo,知道出现bug之后才明白了它的不完善地方.它的比较方式是按照字符串的比较来执行的,所以它有不正确的地方.举个例子,之前客户端版本号为: ...
- ArrayList 、Vector、 LinkList
public class TestList { public static void init(List list) { if(list!=null) ...
- [Locked] Paint House I & II
Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...
- oracle的nvl函数的使用解析
Oracle的Nvl函数 nvl( ) 函数 从两个表达式返回一个非null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExpre ...
- Python进程、线程、协程详解
进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源的管理和分配.任务的调度. ...
- ok6410 u-boot-2012.04.01移植六完善MLC NAND支持
继ok6410 u-boot-2012.04.01移植四.五后,开发板基本已支持MLC NAND,支持DM9000.但是通过NAND命令更新u-boot到NAND,还存在问题,需要根据u-boot的n ...
- 微信支付bug
1.最基本的操作就是检查各项参数正确2.确保将测试微信号加入测试白名单 3.目录正确:发起授权请求的页面必须是在授权目录下的页面,而不能是存在与子目录中.否则会返回错误,Android返回“Syste ...
- 从零开始学习jQuery-------jQuery元素选择器(三)
下面我们来介绍一下jQuery元素选择器,在Web开发中我们最常用的操作是获取元素,然后对获取的元素进行一系列的操作,jQuery根据获取页面元素的不同,可以将jQuery选择器分为四大类:基本选择器 ...
- 刚入门的easyui
这两天看了下easyui的教学先说说自己的一些小小理解吧! ----在使用easyui中也遇到了一个问题 : Uncaught TypeError:cannot call method ‘offset ...
- JS正则表达式验证账号、手机号、电话、邮箱、货币
验证帐号是否合法验证规则:字母.数字.下划线组成,字母开头,4-16位. function checkUser(str){ var re = /^[a-zA-z]\w{3,15}$/; if(re.t ...