刷题34. Find First and Last Position of Element in Sorted Array
一、题目说明
题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n)。题目的难度是Medium!
二、我的解答
这个题目还是二分查找(折半查找),稍微变化一下。target==nums[mid]后,需要找前面、后面的值是否=target。
一次写出来,bug free,熟能生巧!怎一个爽字了得!
#include<iostream>
#include<vector>
using namespace std;
class Solution{
public:
vector<int> searchRange(vector<int>& nums, int target){
vector<int> res;
if(nums.size()<1){
res.push_back(-1);
res.push_back(-1);
return res;
}
int begin = 0;
int end = nums.size()-1;
int mid = -1;
while(begin <= end){
mid = (begin + end) / 2;
if(nums[mid] == target){
begin = mid;
while(begin>0 && nums[begin] == target){
begin--;
}
if(nums[begin]==target){
res.push_back(begin);
}else{
res.push_back(begin+1);
}
end = mid;
while(end<nums.size()-1 && nums[end] == target){
end++;
}
if(nums[end]==target){
res.push_back(end);
}else{
res.push_back(end-1);
}
return res;
}else if(nums[mid] < target){
begin = mid + 1;
}else{
end = mid - 1;
}
}
//未找到
res.push_back(-1);
res.push_back(-1);
return res;
}
};
int main(){
Solution s;
vector<int> nums = {5,7,7,8,8,10};
vector<int> r = s.searchRange(nums,8);
for(vector<int>::iterator it=r.begin();it!=r.end();it++){
cout<<*it<<" ";
}
r = s.searchRange(nums,6);
for(int i=0;i<r.size();i++){
cout<<r[i]<<" ";
}
return 0;
}
代码性能:
Runtime: 12 ms, faster than 38.75% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 10.4 MB, less than 70.33% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
三、改进
上一个题目,发现mid = begin + (end - begin) / 2; ,性能比mid = (begin + end) / 2高很多。
性能提高到:
Runtime: 8 ms, faster than 86.11% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 10.4 MB, less than 82.42% of C++ online submissions for Find First and Last Position of Element in Sorted Array.
这究竟为何,哪位大神指导,请指点。不胜感激!!!
此处不要提mid = (begin + end) / 2可能溢出。。。
刷题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 && 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- [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每天一题】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 (JAVA)
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
随机推荐
- category添加属性
category添加属性 面试题 Category的实现原理,以及Category为什么只能加方法不能加属性. Category中有load方法吗?load方法是什么时候调用的?load 方法能继承吗 ...
- [NCTF2019]Fake XML cookbook
0x00 知识点 XXE攻击 附上链接: https://xz.aliyun.com/t/6887 XXE(XML External Entity Injection)全称为XML外部实体注入 XML ...
- java菜鸟第一天
- POJ 2251 Dungeon Master(三维空间bfs)
题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...
- python常用代码、问题汇总
1.生成dataframe数据 5.读取带 ','分隔符的txt文件 4.DataFrame格式数据处理中报错 2.安装库时出现如下错误: 3.得到股票交易日数据 1.生成dataframe数据 im ...
- 深入理解class
一, class和自定义类型的区别: 1,类声明不会被提升. 2,类声明的代码自动运行在严格模式. 3,类的所有方法都是不可枚举的,而自定的方法必须使用Object.defineProperty来设置 ...
- k8s CI/CD--Jenkinsfile例子
试用k8s内部jenkins,并且配置好podtemplate pipeline{ agent any stages{ stage('get the code'){ steps{ ...
- vue中的axios请求
1.get请求 // get this.$axios.get('请求路径') .then(function (response) { console.log(response); // 成功 }) . ...
- Codeforces 1296D - Fight with Monsters
题目大意: n 只怪兽,每只的血量为 h[i] ,你的攻击力为 a ,你的对手攻击力为 b 打每只怪兽时,都是你先出手,然后你的对手出手,这样轮流攻击 如果是你给予了怪兽最后一击,你就能得到一分 你还 ...
- RK3399开发板Android镜像烧写之Windows系统映像烧写
4.1.1 l RKTool 驱动安装(基于迅为iTOP-3399开发板)DriverAssitant_v4.5.zip 文件,打开 驱动安装成功,如下图: 注意事项:1.目前支持的操作系统包括:X ...