[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) 分 ...
随机推荐
- zombodb sql functions 说明
zombodb 提供了好多方便的sql 函数工具类以及帮助函数 查看zombodb 版本zdb.version() select * from zdb.version(); version ----- ...
- 3.3-1933 problem A
#include <stdio.h> int main(void){ int h; while(scanf("%d", &h) != EOF){ * (h-); ...
- MVC Ajax.BeginForm 提交上传图片
吃水不忘挖井人,如果对你有帮助,请说声谢谢.如果你要转载,请注明出处.谢谢! 异步提交时,出现图片不能上传. 起初我定格在 System.Web.Mvc 中.查询源码时,也是没有问题的.那问题出现 ...
- WampServer的下载方法
http://www.wampserver.com/ 无法访问 报网络连接错误 2019.01.13 最近要用到Windows+apache+mysql+php,为了追求更快的实现速度和更高的稳定性, ...
- Kafka介绍与消息队列
消息队列的好处: 消息队列(Message Queue) 消息: 网络中的两台计算机或者两个通讯设备之间传递的数据.例如说:文本.音乐.视频等内容. 队列:一种特殊的线性表(数据元素首尾相接),特殊之 ...
- 移动端使用mint-ui loadmore实现下拉刷新上拉显示更多
前序:在使用vue做一个h5项目的时候,需要上拉分页加载,实践中总结一下: 首先要安装mint-ui npm i mint-ui -S 然后引入,一般在main.js里面 import Vue fro ...
- 一入爬虫深似海,从此游戏是路人!总结我的python爬虫学习笔记!
前言 还记得是大学2年级的时候,偶然之间看到了学长在学习python:我就坐在旁边看他敲着代码,感觉很好奇.感觉很酷,从那之后,我就想和学长一样的厉害,就想让学长教我,请他吃了一周的饭,他答应了.从此 ...
- Windows下文件加固
今天学到一种Windows下简单的文件加固方法,可以防止文件被(普通)删除. CMD找到要加固的文件. 例:C盘下有个 1516.txt 文本文件需要加固. 然后 copy 该文件.(注意:这里并非普 ...
- cookies的常见方式
cookie有如下特点 保存在客户端,一般由浏览器负责存储在本地. 通常是加密存储的,不过由于存储在本地,很难保证数据不被非法访问,并不怎么安全,所以cookies中不宜保存敏感信息,如密码等. 哪些 ...
- shareSdk分享图片
/** * 只分享图片 * * @param bitmap bitmap */ private void sharedImage(Bitmap bitmap) { RxPermissions rxPe ...