34. Find First and Last Position of Element in Sorted Array (JAVA)
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) {
int[] ret = new int[2];
ret[0] = binaryLeftSearch(nums,target,0,nums.length-1);
ret[1] = binaryRightSearch(nums,target,0,nums.length-1);
return ret;
} public int binaryLeftSearch(int[] nums, int target, int left, int right){
if(left > right) return -1; int mid = left + ((right-left)>>1);
int mostLeft;
if(target <= nums[mid]) {
mostLeft = binaryLeftSearch(nums,target,left,mid-1);
if(mostLeft == -1 && target==nums[mid]) mostLeft = mid;
}
else{ //target > nums[mid]
mostLeft = binaryLeftSearch(nums,target,mid+1,right);
}
return mostLeft;
} public int binaryRightSearch(int[] nums, int target, int left, int right){
if(left > right) return -1; int mid = left + ((right-left)>>1);
int mostRight;
if(target >= nums[mid]) {
mostRight = binaryRightSearch(nums,target,mid+1,right);
if(mostRight == -1 && target==nums[mid]) mostRight = mid;
}
else{ //target < nums[mid]
mostRight = binaryRightSearch(nums,target,left,mid-1);
}
return mostRight;
}
}
34. Find First and Last Position of Element in Sorted Array (JAVA)的更多相关文章
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 刷题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 == [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 ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 34. Find First and Last Position of Element in Sorted Array
1. 原始题目 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在 ...
随机推荐
- Qt 静态库与共享库(动态库)共享配置的一个小办法
对于用 QtCreator 编写静态库,动态库,如何能够以最小的改动, 方便的实现两种形式的库文件生成:可以这麽做: 1)使用想到建立静态库 2)在项目配置文件*.pro 中: TARGET = n ...
- Windows 下安装Apache web服务器
1.Apache 服务器的下载 进入下载页面:http://httpd.apache.org/download.cgi 为提高下载速度,镜像选择清华大学的服务器(http://mirrors.tuna ...
- Ubuntu18.04修改为阿里云
对源安装时,要先知道系统的版本,以免安装错的版本 使用命令:lsb_release -c 备份原先的配置文件 cd /etc/apt sudo cp sources.list sources.list ...
- MVC的各个部分都有那些技术来实现?如何实现?
MVC是Model-View-Controller的简写. Model 代表的是应用的业务逻辑(通过JavaBean,EJB组件实现), View 是应用的表示面(由JSP页面产生), Control ...
- QBXT Day 5图论相关
图论是NOIP的一个非常重要的考点,换句话说,没有图论,NOIP的考纲就得少一大半(虽然很NOIP没有考纲) 图论这玩意吧,和数论一样是非常变态的东西,知识点又多又杂,但是好在一个事,他比较直观比较好 ...
- EMQ插件通过HTTP连接认证服务器实现认证
需求 在EMQ中添加认证插件,将到来的MQTT连接的ClientID.UserName.Password通过HTTP协议发送到认证服务器,用返回的数据决定是否允许该连接: 在连接时和断开时向服务器发送 ...
- Win7上防火墙开放FTP服务以及ping解决方案
1.windows 防火墙开放ftp服务 The following 4 steps will allow both non-secure and SSL FTP traffic through fi ...
- java网络通信:同步阻塞式I/O模型(BIO)
缺点:一个线程只能处理一个客户端连接 服务端: public class TimeServer { public static void main(String[] args) throws IOEx ...
- Linux_Samba详解
目录 目录 Samba Server Parameter Configuration file explain Setup the Samba Server Access the samba shar ...
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_02.三层架构和ssm框架的对应关系