题目链接

Find First and Last Position of Element in Sorted Array - LeetCode

注意点

  • nums可能为空
  • 时间复杂度为O(logn)

解法

解法一:最普通的二分搜索,先找到一个target,然后向两边拓展。

class Solution {
public:
int binarySearch(vector<int>& nums, int target)
{
int left = 0,right = nums.size()-1;
while(left <= right)
{
int mid = left + (right-left)/2;
if(nums[mid] == target) return mid;
if(nums[mid] < target) left = mid+1;
else right = mid-1;
}
return -1;
}
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int index = binarySearch(nums,target);
int left = index,right = index;
while(left > 0 && nums[left-1] == nums[index]) --left;
while(right < nums.size()-1 && nums[right+1] == nums[index]) ++right;
ret.push_back(left);
ret.push_back(right);
return ret;
}
};

解法二:解法一在最坏情况下时间复杂度是O(n),比如整个nums都是target。因此我们用两次二分搜索,第一次找到第一个等于target的数字,第二次找到最后一个等于target的数字。时间复杂度O(logn)

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int left = 0,right = nums.size()-1;
while(left <= right)
{
int mid = left+(right-left)/2;
if(nums[mid] >= target) right = mid-1;
else left = mid+1;
}
if(left >= 0 && left < nums.size() && nums[left] == target) ret.push_back(left);
else return {-1,-1};
left = 0;right = nums.size()-1;
while(left <= right)
{
int mid = left+(right-left)/2;
if(nums[mid] <= target) left = mid+1;
else right = mid-1;
}
if(right >= 0 && right < nums.size() && nums[right] == target) ret.push_back(right);
return ret;
}
};

小结

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

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

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

  2. leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array

    leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array Given an array of int ...

  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. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

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

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

  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: Find First and Last Position of Element in Sorted Array

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

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

  9. (二分查找 拓展) 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 ...

随机推荐

  1. Hbase RESTFul API创建namespace返回500

    1.使用官方提供的/namespaces/namespace创建namespace失败,返回500,官方提供示例:/namespaces/namespace POST 创建一个新的namespace. ...

  2. 如何布局您的PC站和移动站,并表达两者之间内容的对应关系

      如何布局您的PC站和移动站,并表达两者之间内容的对应关系 目前较流量的PC站与移动站配置方式有三种,百度站在搜索引擎角度将这三种分别称为跳转适配.代码适配和自适应,以下为这三种配置方式的名词解释及 ...

  3. Spring入门学习笔记(2)——基于Java的配置

    目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...

  4. kali linux执行apt-get update失败(数字签名过期)

    想要安装某个软件,执行apt-get update 失败,出现下面的错误: 自己查看了更新源是没有问题的,根据提示的错误google了一下,发现是数字签名过期了. 执行下面命令: apt-key ad ...

  5. Kubernetes探索学习002--Kubernetes的基本使用

    Kubernetes 的基本使用方法 原则:使用YAML文件描述你要部署的API对象! 以部署nginx静态站点为例,具体操作及内容如下 1.编写YAML文件 [root@kubernetes01 ~ ...

  6. Refs 和 DOM

    在常规的 React 数据流中,props 是父组件与子组件交互的唯一方式.要修改子元素,你需要用新的 props 去重新渲染子元素.然而,在少数情况下,你需要在常规数据流外强制修改子元素.被修改的子 ...

  7. 最新Python笔试题2017 涵盖知识面广泛

    引言 想找一份Python开发工作吗?那你很可能得证明自己知道如何使用Python.下面这些问题涉及了与Python相关的许多技能,问题的关注点主要是语言本身,不是某个特定的包或模块.每一个问题都可以 ...

  8. nginx keepalived 高可用方案(转)

    转自: https://www.cnblogs.com/leeSmall/p/9356535.html 一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含 ...

  9. 探路者-Beta发布中间产物

    版本控制 版本控制报告:http://www.cnblogs.com/linym762/p/7881047.html git地址:https://git.coding.net/clairewyd/to ...

  10. 20135234mqy-——信息安全系统设计基础第六周学习总结

    处理器体系结构 4.1 Y86指令集体系结构 4.1.1程序员可见状态 Y86程序中的每条指令都会读取或修改处理器状态的某些部分,称为程序员可见状态. 4.1.2 Y86指令 4个指令:irmovl, ...