(二分查找 拓展) 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 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]
------------------------------------------------------------------------------------
这个题是在leetcode和lintcode 上都有的,只不过题目里面给的参数有些是不同的,需要注意的,不过代码是基本相同的。
用遍历数组的方式可能会TLE,所以用二分更好。
emmm,二分查找也可以用递归方式来写的。当用二分查找方式得到一个不为-1数组下标时,向两边扩展,最终得到一个范围。
C++代码:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int ans = search(nums,,nums.size()-,target);
if(ans == -) return {-,-}; //归结根底,vector就是一个数组,可以用{}。
int left = ans,right = ans;
while(left > && nums[left-] == nums[ans]) left--;
while(right < nums.size() - && nums[right+] == nums[ans]) right++;
return {left,right};
}
int search(vector<int>& nums,int left,int right,int target){
if(left > right) return -;
int mid = left + (right - left)/;
if(nums[mid] == target) return mid;
if(nums[mid] > target) return search(nums,,mid-,target);
else return search(nums,mid+,right,target);
}
};
还有一个解法:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int ans = search(nums,,nums.size()-,target);
if(ans == -) return {-,-};
int left = ans,right = ans;
while(left > && nums[left-] == nums[ans]) left--;
while(right < nums.size() - && nums[right+] == nums[ans]) right++;
return {left,right};
}
int search(vector<int>& nums,int left,int right,int target){
// if(left > right) return -1;
// int mid = left + (right - left)/2;
// if(nums[mid] == target) return mid;
// if(nums[mid] > target) return search(nums,0,mid-1,target);
// else return search(nums,mid+1,right,target);
if(nums.size() == ) return -;
while(left + < right){
int mid = left + (right - left)/;
if(nums[mid] > target) right = mid;
else if(nums[mid] < target) left = mid;
else return mid;
}
if(nums[left] == target) return left;
if(nums[right] == target) return right;
return -;
}
};
emmm,也可以看官方题解:https://leetcode.com/articles/find-first-and-last-position-element-sorted-array/
(二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range的更多相关文章
- [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 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- [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
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).题 ...
- 34. Find First and Last Position of Element in Sorted Array + 二分
题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...
- 【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 ...
随机推荐
- Clickhouse v18编译记录
简介 ClickHouse是"战斗民族"俄罗斯搜索巨头Yandex公司开源的一个极具"战斗力"的实时数据分析数据库,是面向 OLAP 的分布式列式DBMS,圈内 ...
- 高德地图 Service 创建服务 USERKEY_PLAT_NOMATCH
在使用高的地图 创建服务的时候 { "errmsg": "USERKEY_PLAT_NOMATCH", "errcode": 10009, ...
- Windows server2008R2 企业内部搭建虚拟专用网络服务
VPN英文全称是“Virtual Private Network”,就是“虚拟专用网络”.可以远程帮助用户.分公司.商业伙伴及供应商同公司的内部网建立可信的安全连接,用于经济有效地连接到商业伙伴和用户 ...
- 完成一个java项目需要的一些基础
包括 1.eclipse关键字 2.文档注 3. jar包的导出与导入 一.elipse关键字 ctry+t 查看父类 ctry+鼠标光标 查看源代码 二.文 ...
- MySQL之视图、触发器、事务、存储过程、函数
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- C. Maximal Intersection(STL)
这道题,关键在于怎么求多个区间的交集,使用multiset就可以 分别将 r , l 存在不同的mutiset中. 然后,我们来看一下 是不是 交集的 l 是最大的, 交集的 r 是最小的 #incl ...
- Kafka 详解(三)------Producer生产者
在第一篇博客我们了解到一个kafka系统,通常是生产者Producer 将消息发送到 Broker,然后消费者 Consumer 去 Broker 获取,那么本篇博客我们来介绍什么是生产者Produc ...
- 静态代理与JDK动态代理
demo地址: https://github.com/ZbLeaning/leaning 代理: 为其他对象提供一种代理以控制对这个对象的访问.分为静态代理和动态代理.代理模式的目的就是为真实业务对象 ...
- input 各种限制
test 1.限制只能输入或黏贴11位长度的数字 <input onkeyup="this.value=this.value.replace(/\D/g,'')" onaft ...
- 06 Django REST Framework 版本控制
01-版本控制 对接口进行版本控制只是一种杀死已部署客户端的“礼貌”方式. - 罗伊菲尔丁. 1. API版本控制允许您更改不同客户端之间的行为.REST框架提供了许多不同的版本控制方案. 2. 版本 ...