leetcode-algorithms-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 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].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

解法

二分法查找

class Solution
{
public:
vector<int> searchRange(vector<int>& nums, int target)
{
vector<int> target_pos(2, -1);
int min = 0;
int max = nums.size() - 1;
int mid = 0;
int t = 0;
while(min <= max)
{
mid = (min + max) / 2;
t = nums[mid];
if (t == target)
{
target_pos.clear();
int pre = mid - 1;
bool have = false;
while(pre >= min && nums[pre] == target)
{
have = true;
--pre;
}
if (have) target_pos.push_back(++pre);
else target_pos.push_back(mid);
have = false;
pre = mid + 1;
while (pre <= max && nums[pre] == target)
{
have = true;
++pre;
}
if (have) target_pos.push_back(--pre);
else target_pos.push_back(mid);
break;
}
else if (t > target)
{
max = mid - 1;
}
else
{
min = mid + 1;
}
}
return target_pos;
}
};

时间复杂度: O(logn).

空间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array的更多相关文章

  1. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

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

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

  3. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  4. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  5. [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 ...

  6. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  7. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

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

  8. [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 ...

  9. 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 ...

  10. leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array

    思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...

随机推荐

  1. @PathVariable与@RequestBody的区别,及前段请求接口的写法。

    @PathVariable 1:接受参数时,地址栏为:/{args1}/{args2} 2:用法:(@PathVariable(value = "args")Long  id) 3 ...

  2. 2-4、nginx特性及基础概念-nginx web服务配置详解

    Nginx Nginx:engine X 调用了libevent:高性能的网络库 epoll():基于事件驱动event的网络库文件 Nginx的特性: 模块化设计.较好扩展性(不支持模块动态装卸载, ...

  3. Linux命令之nl命令

    nl 命令在 Linux 系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号,其默认的结果和 与 cat -n 有点不太一样,nl 可以将行号做比较多的显示设计,包括位数是否自动补齐 ...

  4. ECharts 使用总结

    1.去掉Echarts 图标上边框和右边框 option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, grid: { show: 'true', ...

  5. 【Mysql】外键

    MYSQL数据表建立外键 MySQL创建关联表可以理解为是两个表之间有个外键关系,但这两个表必须满足三个条件 1.两个表必须是InnoDB数据引擎 2.使用在外键关系的域必须为索引型(Index) 3 ...

  6. Vue运行报错--not defined

    按F12键进入调试模式,谷歌总是提示Uncaught ReferenceError: ——is not defined这个错误. 原来是因为虽然是传递的值,但是在函数传参的时候也要加引号,加上引号后就 ...

  7. Codeforces 629 E. Famil Door and Roads

    题目链接:http://codeforces.com/problemset/problem/629/E 询问这个简单环的期望.考虑将这个环拆成两部分. 令${deep[x]>=deep[y]}$ ...

  8. 深入浅析Spring的AOP实现原理

    转载来源:https://www.jb51.net/article/81788.htm AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Or ...

  9. Inception Network

    1. 下图为一个Inception 模块,即将输入的图像通过多种过滤器或者池化操作后,全部再给拼起来形成新的图像. 2. Inception 网络就是讲将多个Inception模块连起来而已,如下图的 ...

  10. HRBUST - 2358 Magic network

    HRBUST - 2358 思路:dfs序 + 树状数组 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimiz ...