[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 a given target
value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
题意:
给定一个有序数组,找出某个值的起始和终止区间。
思路:
二分查找
代码:
class Solution {
public int[] searchRange(int[] nums, int target) {
// corner case
if(nums == null || nums.length == 0) return new int[]{-1,-1};
int left = findFirst(nums, 0, nums.length-1, target);
if(left == -1) return new int[]{-1,-1};
int right = findLast(nums, 0, nums.length-1, target);
return new int[]{left, right}; }
// find start point
private int findFirst(int[] nums, int left, int right, int target) {
// avoid dead looping
while(left + 1 < right){
// avoid overflow
int mid = left + (right - left)/2;
// left------ |mid| ---target---right
if(nums[mid] < target){
left = mid;
}
// left---target---|mid| ------right
else{
right = mid;
}
}
if (nums[left] == target) return left;
if (nums[right] == target) return right;
return -1;
} private int findLast(int[] nums, int left, int right, int target) {
while(left + 1 < right){
int mid = left + (right - left)/2;
// left---target---|mid| ------right
if(nums[mid] > target){
right = mid;
}
// left------ |mid| ---target---right
else{
left = mid;
}
}
if(nums[right] == target) return right;
if (nums[left] == target) return left;
return -1;
}
}
[leetcode]34.Find First and Last Position of Element in Sorted Array找区间的更多相关文章
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- [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 ...
- [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 && 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 ...
- 刷题34. Find First and Last Position of Element in Sorted Array
一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...
- 【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
思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...
- 34. Find First and Last Position of Element in Sorted Array + 二分
题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...
随机推荐
- 笔记本使用control线连接交换机
要求: 1.一台笔记本 2.一条usb转rj45串口线 (一端是usb口一端是网口) 连接步骤: usb口插入笔记本,网口插入交换机控制口(交换机上面一般会有标注) 直连步骤: 首先查看是哪个com口 ...
- nginx屏蔽某段IP、某个国家的IP
nginx中可通过写入配置文件的方法来达到一定的过滤IP作用,可使用deny来写. deny的使用方法可用于前端服务器无防护设备的时候过滤一些异常IP,过滤的client ip会被禁止再次访问,起到一 ...
- Hanlp中N最短路径分词详细介绍
N-最短路径 是中科院分词工具NLPIR进行分词用到的一个重要算法,张华平.刘群老师在论文<基于N-最短路径方法的中文词语粗分模型>中做了比较详细的介绍.该算法算法基本思想很简单,就是给定 ...
- 关于rtsp的时间戳问题
这里主要关注的rtp包的时间戳,在rtsp中,播放器的1S钟的定义是和媒体的采样率有关的. 例如视频的采样率是90K,那么最小时间粒度(单位)是1/90000秒,再转换成ms就是 1/90毫秒,这个就 ...
- yum安装下的nginx,如何添加模块,和添加第三方模块
需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...
- Group Pathfinding & Movement in RTS Style Games
转自:http://gamasutra.com/blogs/AndrewErridge/20180522/318413/Group_Pathfinding__Movement_in_RTS_Style ...
- hadoop2.6.4集群笔记
---恢复内容开始--- 一,linux下的准备工作 1,修改主机名: vi /etc/sysconfig/network 2,修改ip vi /etc/sysconfig/network-scrip ...
- android 开发 我的高德地图代码例子
下载高德地图依赖库和相关注册方式,请查看高德开发者网站:http://lbs.amap.com/api/android-sdk/summary 点击打开链接 高德地图坐标拾取器:http://lbs ...
- 【亲测】解决虚拟机CentOS7联网ping不通相关问题(通俗易懂)
对于是使用windows操作系统的小伙伴来说(mac用户忽略),要学习一些技术可能需要使用Linux系统,自然就需要使用虚拟机安装Linux,当然现在很多主流的学习网站上的教程都会提供老师配置好的虚拟 ...
- 《从零玩转python+人工智能-3》120,122节课深度优先疑问解答
深度优先(从左往右): 按照这个原则来:至于使用栈,或者队列:根据它们不同的特性:最终务必保证最终结果是原继承结构的“从左往右”:所以,如果是栈,就是右侧先入栈,左侧再入(这样左侧能先出来,遵循从左 ...