(二分查找 拓展) 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 ...
随机推荐
- 智能POS如何获取日志&数据库文件
使用Teamviewer连接安卓机器,文件传输 ================================================================== 智能POS查看数据 ...
- Python ——报错集锦
https://blog.csdn.net/weixin_42660771/article/details/80990665 错误(1):SyntaxError:'return' outside fu ...
- Linux分页机制之概述--Linux内存管理(六)
1 分页机制 在虚拟内存中,页表是个映射表的概念, 即从进程能理解的线性地址(linear address)映射到存储器上的物理地址(phisical address). 很显然,这个页表是需要常驻内 ...
- Redhat安装Oracle 11g (转)
1.1 安装前准备 1.1.1 修改操作系统核心参数 在Root用户下执行以下步骤: 1.1.1.1 修改/etc/security/limits.conf文件 输入命令:vi /et ...
- 微信小程序——页面跳转及传参
小程序页面跳转 微信小程序的页面跳转依然是以传统的请求转发和请求重定向为主,tabbar的存在,有TAB页面的跳转. 为了微信小程序的简介方便,规定页面路径只能是十层,应尽量避免过多的交互方式. 1. ...
- springboot use
https://github.com/ityouknow/spring-boot-examples http://www.ityouknow.com/springboot/2017/06/26/spr ...
- Luogu P5296 [北京省选集训2019]生成树计数
Luogu P5296 [北京省选集训2019]生成树计数 题目链接 题目大意:给定每条边的边权.一颗生成树的权值为边权和的\(k\)次方.求出所有生成树的权值和. 我们列出答案的式子: 设\(E\) ...
- debian8下acme nginx 部署记录
1.更新源 apt update 2.安装curl git apt install curl git -y 3.克隆acme仓库 curl https://get.acme.sh | sh git c ...
- python学习之类的反射
在学习网络编程的时候用到反射,然后发现自己反射这部分的应用不是很熟练,决定返回来重新整理一下 对于类的反射,主要有四个用法,下面都说一下 1.hasattr 判断对象或者类是否存在指定的属性,看代码以 ...
- 转://Oracle数据库升级后保障SQL性能退化浅谈
一.数据库升级后保障手段 为了保障从10.2.0.4版本升级到11.2.0.4版本更加平稳,我们事先采用了oracle性能分析器(SQL Performance Analyzer)来预测数据库的关键S ...