乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪: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的更多相关文章
- 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- [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 ...
- 【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 ...
- C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...
- (二分查找 拓展) 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 ...
- [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 ...
- [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 ...
随机推荐
- rails中accepts_nested_attributes_for应用
Model: class Blog < ActiveRecord::Base has_many :strip_rules accepts_nested_attributes_for :strip ...
- SSL编程(3).NET实现SSL服务端
准备开发用数字证书 一般学习和开发调试场合,不会随便使用正式的SSL服务器证书的私钥.由于服务器验证对于SSL来说是必须的,SSL服务器端必须有拥有一个服务器 证书,即能够访问到证书的私钥.对于要 ...
- 简单实现Spring框架--注解版
自己写的Spring框架——简单实现IoC容器功能 前几天在网上看了篇帖子,是用xml的方式实现spring的ioc容器,觉得挺有意思的,这边自己试着用注解的形式造了一套轮子. 工程结构 codein ...
- 手机调试 --- 通过chrome测试手机网站
移动端有时候我们要调试手机网站. Chrome怎么调试手机页面呢? 毕竟有时候手机支持的JS度跟PC不一样.最开始就遇见了.手机端浏览器不支持执行string.includes. PC端支持该函数, ...
- Ubuntu安装完成后设置root密码
安装完ubuntu后没有默认的root密码,如果要设置root密码需要进行如下步骤: 1 sudo passwd 2 连续输入两次新密码
- javascript: 数组详细操作方法及解析合集(9个改变8个不变12个遍历)
改变原数组的方法(9个): 1 2 3 4 5 let a = [1,2,3]; ES5: a.pop()/ a.shift()/ a.push()/ a.unshift()/ a.reverse() ...
- 【代码笔记】iOS-My97DatePicker日历
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 3.1 - Apps or metadata that mentions the name of any other mobile platform will be rejected
3.1 - Apps or metadata that mentions the name of any other mobile platform will be rejected3.1 Detai ...
- [可能不知道]什么是PeopleSoft的JOLT以及相关进程
PeopleSoft applecation server依赖于Jolt,Jolt是与Tuxedo配套的产品,可以处理所有web请求.换句话说,Jolt是application server与web ...
- Linux下安装VSCode
进行下载 64位的包:地址: https://code.visualstudio.com/docs/?dv=linux64&build=insiders 1.解压: tar -zxvf cod ...