给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

你的算法时间复杂度必须是 O(log n) 级别。

如果数组中不存在目标值,返回 [-1, -1]。

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8 输出: [3,4]

示例 2:

输入: nums = [5,7,7,8,8,10], target = 6 输出: [-1,-1]

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target)
{
int len = nums.size();
vector<int> res;
int low = 0;
int high = len - 1;
while(low <= high)
{
int mid = (low + high) / 2;
if(nums[mid] == target)
{
int start;
int end;
int i;
for(i = mid; i >= 0; i--)
{
if(nums[i] != target)
break;
}
start = ++i;
for(i = mid; i < len; i++)
{
if(nums[i] != target)
break;
}
end = --i;
res = {start, end};
return res;
}
else if(nums[mid] > target)
{
high = mid - 1;
}
else
{
low = low + 1;
}
}
res = {-1, -1};
return res;
}
};

Leetcode34.Find First and Last Position of Element in Sorted Array在排序数组中查找元素的位置的更多相关文章

  1. 【LeetCode每天一题】Find First and Last Position of Element in Sorted Array(找到排序数组中指定元素的开始和结束下标)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  2. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  3. [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  4. C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...

  5. leetcode34. Find First and Last Position of Element in Sorted Array

    二分查找不只是查找,还可以根据需求添加条件进行查找,比如这个题,左端点的条件就是边界点或者小于target,右端点的条件就是!=size()或者大于.根据这个找到查找的条件

  6. LeetCode34.在排序数组中查找元素的第一个和最后一个位置 JavaScript

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...

  7. leetcode34. 在排序数组中查找元素的第一个和最后一个位置

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...

  8. 【Java实现】剑指offer53.1——在排序数组中查找数字(LeetCode34:在排序数组中查找元素的起始位置)

    序数组中查找元素的起始位置):思路分享 <剑指offer>题目和LeetCode主站本质是一样的,想要找到target数目,也需要找到左右边界 题目解析: 在一个排序数组中,找到targe ...

  9. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

随机推荐

  1. Chrome快捷键, Mac 下 Chrome 浏览器 快捷键

    Chrome窗口和标签页快捷键:Ctrl+N 打开新窗口 Ctrl+T 打开新标签页 Ctrl+Shift+N 在隐身模式下打开新窗口 Ctrl+O,然后选择文件 在谷歌浏览器中打开计算机上的文件 按 ...

  2. 软件-版本控制器-VisualSVN:VisualSVN

    ylbtech-软件-版本控制器-VisualSVN:VisualSVN VisualSVN 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https:/ ...

  3. 基于vue-cli的vs code设置

    vue-cli自带eslin校验,vs code采用下可以设置在保存文件时会自动纠正格式 { // vscode默认启用了根据文件类型自动设置tabsize的选项 "editor.detec ...

  4. 使用xshell远程连接到linux

      1.检查是否安装ssh rpm -qa | grep ssh 已安装是这样 如果没有安装,则 yum install openssh* #命令安装 2.开启ssh服务 [root@localhos ...

  5. 《DSP using MATLAB》Problem 8.4

    今天是六一儿童节,陪伴不了家人,心里思念着他们,看着地里金黄的麦子,远处的山,高高的天 代码: %% ------------------------------------------------- ...

  6. history配置

    /etc/profile配置文件中,末尾增加如下参数项: HFILE=`who -m | awk '{print $1}'`readonly HISTFILE=/var/history/$HFILE- ...

  7. [转]基于MefBootstrapper的Bootstrapper

    public class Bootstrapper : MefBootstrapper    {        ModuleCatalog moduleCatalog; protected overr ...

  8. __name__ = '__main__'

    有句话经典的概括了这段代码的意义: “Make a script both importable and executable” 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可 ...

  9. httpclient遇到java.net.URISyntaxException: Illegal character in scheme name at index 0:

    正准备按照大佬的解决办法处理, 看会一条回复,说url有空格 检查了一下,还真是有空格 去除url中的空格,问题解除

  10. Python Flask高级编程之从0到1开发《鱼书》精品项目

    Python Flask高级编程之从0到1开发<鱼书>精品项目     整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感 ...