[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) 分 ...
随机推荐
- python 常用的模块
面试的过程中经常被问到使用过那些python模块,然后我大脑就出现了一片空白各种模块一顿说,其实一点顺序也没有然后给面试官造成的印象就是自己是否真实的用到这些模块,所以总结下自己实际工作中常用的模块: ...
- 【动态规划】最大连续子序列和,最大子矩阵和,最大m子段和
1.最大字段和问题 求一个序列最大连续子序列之和. 例如序列[-1,-2,-3,4,5,-6]的最大子段和为4 + 5 = 9. ①枚举法 int MaxSum(int n,int *a){ int ...
- 廖雪峰Java6 IO编程-3Reader和Writer-1Reader
1.java.io.Reader和java.io.InputStream的区别 InputStream Reader 字节流,以byte为单位 字符流,以char为单位 读取字节(-1,0-255): ...
- KPPW2.5 漏洞利用--CSRF
kppw2.5 CSRF漏洞复现 漏洞说明 http://192.168.50.157/kppw25/index.php?do=user&view=message&op=send 收件 ...
- 快速傅立叶变换(FFT)算法
已知多项式f(x)=a0+a1x+a2x2+...+am-1xm-1, g(x)=b0+b1x+b2x2+...+bn-1xn-1.利用卷积的蛮力算法,得到h(x)=f(x)g(x),这一过程的时间复 ...
- Vue keep-alive的总结
1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗. <keep-alive> <component> <!- ...
- php 对象转字符串
$json_string = json_encode($object, JSON_FORCE_OBJECT); json_encode($object); //结果:"[{"aa& ...
- 【待考察】Appium使用技巧,助你快速入门移动端自动化!
Appium使用技巧,助你快速入门移动端自动化! 原创: 柠檬班superman 柠檬班软件测试 1月4日 关注并置顶[柠檬班]的小哥哥小姐姐 “猪”年行大运 说说最近研究移动端的自动化 移动端的自动 ...
- android 开发 View _13 绘制图片与BitmapShader位图的图像渲染器
BitmapShader位图的图像渲染器 TileMode 模式 Shader.TileMode.CLAMP 边缘拉伸. Shader.TileMode.MIRROR 在水平方向和垂直方向交替景象, ...
- Centos nginx安装
1.下载nginx http://nginx.org/en/download.html 2.上传到服务器上,并解压: rz 后选择上传的文件 tar -zxvf /fish/download/ngin ...