给定一个按照升序排列的整数数组 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. 杂项-公司:Google

    ylbtech-杂项-公司:Google 谷歌公司(Google Inc.)成立于1998年9月4日,由拉里·佩奇和谢尔盖·布林共同创建,被公认为全球最大的搜索引擎公司.谷歌是一家位于美国的跨国科技企 ...

  2. mongdb 使用聚合函数异常

    异常信息: Command execution failed:  Error [The 'cursor' option is required, except for aggregate with t ...

  3. 2016 CCPC网络选拔赛 部分题解

    HDU 5832 - A water problem 题意:有两颗星球,一年的长度分别为37天和173天.问第n天时它们是否为新年的第一天. 思路:显然  n 同时被37和173整除时,两种历法都在新 ...

  4. IDEA(2018)连接MySQL数据库失败的解决方法(报错08001)

     解决方法: 将url改成: jdbc:mysql://localhost:3306/studentmanage?useSSL=true&serverTimezone=Hongkong& ...

  5. python流程控制-条件语句If,while循环

    一.If,条件语句-选择 格式:python简洁优美,注意缩进 1.第一种: if 条件: 四个空格(tab键)  满足条件时的执行步骤 if 5>4 : print(666) print(77 ...

  6. 短URL系统、301/302重定向

    短 URL 系统是怎么设计的?https://yq.aliyun.com/articles/87600 短网址(short URL)系统的原理及其实现 https://hufangyun.com/20 ...

  7. ssh连接Ubuntu之access denied

    解决方法是: 修改/etc/ssh/ssh_config文件, #PermitRootLogin prohibit-password改为PermitRootLogin yes 然后 PasswordA ...

  8. js 实现横向轮播效果

    参考:https://www.cnblogs.com/LIUYANZUO/p/5679753.html html: <!DOCTYPE html> <html> <hea ...

  9. elasticsearch 中文API 删除(四)

    删除API 删除api允许你通过id,从特定的索引中删除类型化的JSON文档.如下例: DeleteResponse response = client.prepareDelete("twi ...

  10. 深度神经网络Google Inception Net-V3结构图

    深度神经网络Google Inception Net-V3结构图 前言 Google Inception Net在2014年的 ImageNet Large Scale Visual Recognit ...