题目链接

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. vue 使用ref获取DOM元素和组件引用

    在vue中可以通过ref获取dom元素,并操作它,还可以获取组件里面的数据和方法. HTML部分: <div id="app"> <input type=&quo ...

  2. Python处理PDF和Word文档常用的方法(二)

    Python处理word时,需要安装和导入python-docx模块. 安装命令:pip install python-docx 导入命令:import docx 编码编写顺序:用docx.Docum ...

  3. 学员管理系统(SQLAlchemy 实现)

    一.业务逻辑 二.设计表结构 三.代码结构 start.py import os, sys sys.path.insert(0, os.path.dirname(os.path.dirname(os. ...

  4. python接口测试之requests库(一)

    一.requests库的安装 requests库作为第三方库,需要安装 cmd模式下,运行pip install requests 二.在学习如何发送请求之前,我们先来了解一下requests库,查看 ...

  5. Spring笔记①--helloworld

    Spring Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架,它主要是为了解决企业应用开发的复杂性而诞生的: 目的:解决企业应用开发的复杂性 功能:使用基本的Javabean ...

  6. Leetcode题库——9.回文数

    @author: ZZQ @software: PyCharm @file: HuiWenShu.py @time: 2018/9/16 16:51 要求:判断一个整数是否是回文数.回文数是指正序(从 ...

  7. 学习总结:jQuery插件开发模式和结构

    学习博客链接: ①https://www.cnblogs.com/cyStyle/ ② https://www.cnblogs.com/chengyunshen/p/7277305.html ③ ht ...

  8. LeetCode题解:(139) Word Break

    题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...

  9. 软工网络15团队作业8——Beta阶段敏捷冲刺(Day2)

    提供当天站立式会议照片一张 每个人的工作 1.讨论项目每个成员的昨天进展 赵铭: 根据计划安排,继续学习数据库. 吴慧婷:做Beta阶段的计划,并为界面设计寻找素材,学习界面优化. 陈敏: 根据任务, ...

  10. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...