乘风破浪: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. elasticSearch6源码分析(4)indices模块

    1.indices概述 The indices module controls index-related settings that are globally managed for all ind ...

  2. 版本管理(二)之Git和GitHub的连接和使用

    首先需要注册登录GitHub:https://github.com 然后 ①:下载Git 先从Git官网,由于我的系统是64位的所以选择64-bit Git for Windows Setup htt ...

  3. POJ 1730 Perfect Pth Powers(暴力枚举)

    题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...

  4. 【Leetcode】292. Nim游戏

    题目链接:https://leetcode-cn.com/problems/nim-game/description/ 您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1  ...

  5. Word文档中多个编号放同一行的方法(非技术)

    最近在帮公司出应届生校招面试题,为了方便,选择题部分的答案用了Word的[编号]功能!如下截图所示: 这么简短的四个答案这么竖着放很占空间(打印时也很浪纸张),能不能让它们全部横放在同一行,或两两一组 ...

  6. 一:MyBatis知识整理(1)

    一:MyBatis的架构 1.mybatis配置SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息. mapper.xml文件即sql映射文 ...

  7. Java Service Wrapper--来自官网文件

    -----------------------------------------------------------------------------Java Service Wrapper Pr ...

  8. c# IEnumerable和IEnumerator枚举器

    一 : IEnumerable 公开枚举数,该枚举数支持在非泛型集合上进行简单迭代. IEnumerable是可以枚举的所有非泛型集合的基接口,IEnumerable包含单个方法GetEnumerat ...

  9. 创建Cordova项目 报错Error: Unhandled "error" event

    cordova版本7.0以上版本 创建cordova项目错误信息 Error: Unhandled "error" event. (  Error from Cordova Fet ...

  10. 3.配置Spring+SpringMvc+Mybatis(分库or读写分离)--Intellij IDAE 2016.3.5

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 建立好maven多模块项目后,开始使用ssm传统的框架:http://www.cnblogs.com/yysbol ...