给定一个按照升序排列的整数数组 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. 19-11-12-Aftern-℘

    我饿死了,于是写写博客安慰一下即将退役的自己. ZJ: T1. 三种颜色,想到一道神奇的‘天空龙’. 于是觉得此题可做. 那好了. 于是切掉,还拿了一个暴力对拍.疯狂A. 啊dfs慢的要死了 T2一眼 ...

  2. redis深入学习(三)-----事务、主从复制、jedis

    reids事务 概念 可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序地串行化执行而不会被其它命令插入,不许加塞 作用 一个队列中,一次性.顺序性.排他性的执行一系列 ...

  3. 深入理解JVM(一)类加载器部分、类变量、常量、jvm参数

    类加载概述 在java代码中,类型的加载.连接与初始化过程都是在程序运行期间完成的 类型:class.interface(object本身).类型可在运行期间生成,如动态代理.一种runting概念 ...

  4. PAT甲级——A1088 Rational Arithmetic

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

  5. mysql 乐观判断 校验

    说下场景, 用户账户 有 100 元钱,  他执行了两个操作,  A操作发红包发了80块钱, B操作 发红包 发了 70 ,并发, 假如没有 冻结这一说法,  两个操作都是去 查询余额, 还有100 ...

  6. css 渐变背景

    background: linear-gradient(left,#fa7f6d, #fc5e7f); left: 从左边开始

  7. 微信回调校验失败兼容php7

    今天在移动微信支付的代码的时候,发现校验失败,之前好好的,一点点打印了,顺着微信校验程序打印看结果,发现  $xml = $GLOBALS['HTTP_RAW_POST_DATA'];; 接收到的数据 ...

  8. 【codeforces 500D】New Year Santa Network

    [题目链接]:http://codeforces.com/problemset/problem/500/D [题意] 有n个节点构成一棵树; 让你随机地选取3个不同的点a,b,c; 然后计算dis(a ...

  9. thinkcmf报错:fileowner(): stat failed for /sys

    thinkcmf转移到linux云服务器后,后台更新缓存页面报错,错误信息fileowner(): stat failed for /sys 临时解决办法:修改common.php cmf_clear ...

  10. python 日记 day3

    数据类型的概况:1.int 用于计算. 2.str 用于存储少量数据. 3.list 用于存储大量数据. 4.元祖 又叫只读列表,元素不可更改. 5. dic 用于存储关系型对象 . 6.集合 A.i ...