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

一、前言

这次我们还是要改造二分搜索,但是想法却有一点不一样。

二、Find First and Last Position of Element in Sorted Array

2.1 问题

2.2 分析与解决

查找问题,时间复杂度要求对数级别的,我们自然的想到了二分查找,和上一题一样,需求都是有点不一样的,这次是有重复的数字,找出某一特定的重复的数组的起始和结束位置,我们依旧要是有二分查找,不过,当找到了目标值的时候,不能直接返回,需要判断这个数值的左右是否有同样的数字,如果左右都有,则继续左右搜索,如果左有右没有则记录结束为止并且向左搜索,如果右有左没有,则记录左节点并且向右搜索。

class Solution {
// returns leftmost (or rightmost) index at which `target` should be
// inserted in sorted array `nums` via binary search.
private int extremeInsertionIndex(int[] nums, int target, boolean left) {
int lo = 0;
int hi = nums.length; while (lo < hi) {
int mid = (lo + hi) / 2;
if (nums[mid] > target || (left && target == nums[mid])) {//这一句非常重要
hi = mid;
}
else {
lo = mid+1;
}
} return lo;
} public int[] searchRange(int[] nums, int target) {
int[] targetRange = {-1, -1}; int leftIdx = extremeInsertionIndex(nums, target, true); // assert that `leftIdx` is within the array bounds and that `target`
// is actually in `nums`.
if (leftIdx == nums.length || nums[leftIdx] != target) {
return targetRange;
} targetRange[0] = leftIdx;
targetRange[1] = extremeInsertionIndex(nums, target, false)-1; return targetRange;
}
}

三、总结

通过改造二分搜索,我们可以得到正确的答案,但是我们同样也看到了,算法的简练渡和通用性的重要意义。

乘风破浪:LeetCode真题_034_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. Find First and Last Position of Element in Sorted Array - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...

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

  6. C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...

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

随机推荐

  1. 【IT笔试面试题整理】判断一个二叉树是否是平衡的?

    [试题描述]定义一个函数,输入一个链表,判断链表是否存在环路 平衡二叉树,又称AVL树.它或者是一棵空树,或者是具有下列性质的二叉树:它的左子树和右子树都是平衡二叉树,且左子树和右子树的高度之差之差的 ...

  2. Java-Maven(八):配置远程中央仓库的各种方法

    一.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他 ...

  3. vscode浏览器打开html vscode修改默认浏览器

    vscode怎么浏览器打开html预览?这里大家可以通过安装open in browser插件解决. 1.vscode怎么浏览器预览 1.点击拓展 2.输入open in browser,选择第一个 ...

  4. vector向量容器(常用的使用方法总结)

    关于STL中vector容器的学习,编译运行后边看代码,边看执行结果效果更佳,还是想说看别人的代码一百遍,不如自己动手写一遍. vector向量容器不但能像数组一样对元素进行随机访问,还能随时在尾部插 ...

  5. [转]Magento Configurable Product

    本文转自:https://docs.magento.com/m1/ce/user_guide/catalog/product-configurable.html A configurable prod ...

  6. ExtJs 中Viewport的介绍与使用

    ExtJs 中Viewport的介绍与使用 VeiwPort 代表整个浏览器显示区域,该对象渲染到页面的body 区域,并会随着浏览器显示区域的大小自动改变,一个页面中只能有一个ViewPort 实例 ...

  7. [日常] Go语言圣经--作用域,基础数据类型,整型

    go语言圣经-作用域 1.一个声明语句将程序中的实体和一个名字关联,比如一个函数或一个变量 2.一个变量的生命周期是指程序运行时变量存在的有效时间段;声明语句的作用域对应的是一个源代码的文本区域,它是 ...

  8. Could not transfer artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from/to central

    问题: maven安装完成,环境变量配置没有问题,cmd窗口运行mvn compile的时候报错如下: Plugin org.apache.maven.plugins:maven-resources- ...

  9. 设计模式(18)--Memento(备忘录模式)--行为型

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.模式定义: 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式. ...

  10. bootstrap datetimepicker日期插件美化

    效果 https://segmentfault.com/img/bVbieIp?w=1029&h=461 原文链接:https://segmentfault.com/a/11900000167 ...