博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
34. Search for a Range(C++)
阅读量:4622 次
发布时间:2019-06-09

本文共 922 字,大约阅读时间需要 3 分钟。

Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,

Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

 

Solution:

class Solution {public:    vector
searchRange(vector
& nums, int target) { vector
m_vec(2,-1); bool s_flag=false,e_flag=false; for(int i=0,j=nums.size()-1;i<=j;) { if(nums[i]==target) { m_vec[0]=i; s_flag=true; }else i++; if(nums[j]==target){ m_vec[1]=j; e_flag=true; }else j--; if(s_flag&&e_flag) break; } return m_vec; }};

  

转载于:https://www.cnblogs.com/devin-guwz/p/6696456.html

你可能感兴趣的文章
关于无线的Idle Timeout和Session Timeout
查看>>
jquery对radio和checkbox的操作
查看>>
hadoop1.x和2.x的一些主要区别
查看>>
Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
查看>>
poshytip基本使用
查看>>
codeforces #313 div1 B
查看>>
php 模拟post的新发现,重点在最后的新方法
查看>>
SoapUI5.1.2安装和破解教程
查看>>
动态定时给下拉列表添加数据
查看>>
iOS创建静态库
查看>>
生产系统中 RAC 数据库服务器 不要批量 gzip压缩
查看>>
82. Remove Duplicates from Sorted List II
查看>>
pattern与matcherr
查看>>
《java基础知识》Java 运算符
查看>>
C# 如何实现记住密码功能
查看>>
服务器与wp7的socket通信【转】 http://www.cnblogs.com/linzheng/archive/2011/06/21/2086456.html...
查看>>
oschina 客户端源码解析 【转】http://blog.csdn.net/wangchun8926/article/category/1315637
查看>>
Mac OS删除文件夹和文件的命令
查看>>
MyBatis中<![CDATA[ ]]>的使用
查看>>
SpringBoot+Vue(2)
查看>>