刷题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 ...
随机推荐
- MFC 实现CTreeCtrl单选
void CDepartmenManager::SetUncheck(HTREEITEM hTree) { if (!hTree){ return; } m_DePartmentView.SetChe ...
- export环境变量
/etc/profile和/etc/profile.d/区别 [root@zzx conf]# vim /etc/profile.d/tomcat.sh 添加如下内容再运行脚本就可以添加环境变量 ...
- mybatis环境搭建(eclipse,idea)
基于java配置SSM,eclipse 新建maven,web项目 .... 项目结构: jar包 pom.xml spring和DispatcherServlet上下文 public class D ...
- java中的字符串String
一.String简介d 参考:https://www.cnblogs.com/zhangyinhua/p/7689974.html String类代表字符串. java.lang.String: Ja ...
- C#构造函数调用其他构造函数
http://blog.csdn.net/dogfish/article/details/6990266 <-- 虏来的地 public class Class1 { public Class ...
- VScode 修改中文字体
打开vscode ctrl+,打开设置 找到font,第一个是首选的英文字体,第二个是中文字体.
- [XNUCA2019Qualifier]EasyPHP
0x00 知识点 预期解中知识点: htaccess生效 如果尝试上传htaccess文件会发现出现响应500的问题,因为文件尾有Just one chance 这里采用# \的方式将换行符转义成普通 ...
- 利用zed相机为rtabmap_ros录制rosbag包及其使用
1,录制rosbag包 rosbag record /zed_node/rgb/image_rect_color /zed_node/rgb/camera_info /zed_node/depth/d ...
- 72)MFC测试动态共享库
动态共享库: 首先我建立一个新的动态库: 然后不选择空项目了,因为我们普通的cpp文件 入口是main win32入口是winmain 那么这个动态库的入口在哪里 我们就是为了看一看: 出来这样 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: JSON
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...