刷题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 ...
随机推荐
- 使用jackson转换类型时报Unrecognized field
调用 objectMapper.convertValue(obj, valueType ); 时报错 原因 obj 的属性多于 valueType 导致,添加一条语句即可 objectMapper.c ...
- hex、Base64
一.什么是Hex 将每一个字节表示的十六进制表示的内容,用字符串来显示. 二.作用 将不可见的,复杂的字节数组数据,转换为可显示的字符串数据 类似于Base64编码算法 区别:Base64将三个字节转 ...
- sping--事务
事务的四大特性(ACID): 原子性(Atomicity) 一致性(Consistency) 隔离性(Isolation) 持久性(Durability) 事务属性: 1. propagation : ...
- Spring Tools 4 STS没有创建Dynamic Web Project的选项 以及 Spring Tools 4 STS New 菜单没有Spring Bean Configuration File选项
Spring Tools 4 STS没有创建Dynamic Web Project的选项 STS4默认不带Dynamic Web Project插件. 解决方法:1.打开:Help 选择 Instal ...
- div 100% 填充页面
css中 html,body{ margin:0; padding:0; height:100%; }
- mini2440 裸机程序下载到 sdram 不能运行。
今天在 写了个简单的 led 的汇编程序,下载到 mini2440 的 nand flash 里面可以正常运行,但是下载到 sdram 里面不能运行. 后来发现有几个注意点, 要在 sdram 中运行 ...
- 实验吧web-易-上传绕过
随便上传一个png文件,出现提示 我们再上传一个php文件,却出现提示 上传遇到问题是肯定的,题目就是上传绕过,所以我们下面要做的就是绕过检测. 这里使用00截断. 首先在提交时抓包 我们将图中upl ...
- c++ 正则表达式查找
C++ 正则表达式的使用 需求: 字符串含有除[0-9a-z]之外的字符,均返回失败! #include<regex> smatch result; string reg_str = &q ...
- [CISCN2019 总决赛 Day2 Web1]Easyweb
0x00 知识点 1:备份文件泄露 2:SQL注入 3:php短标签 短标签<? ?>需要php.ini开启short_open_tag = On,但<?= ?>不受该条控制. ...
- Dlib笔记一:基本数据结构和基本操作
编译了Dlib之后就开始想着怎么用起来,先从基本的数据类型说起吧,因为是图像,所以难免会跟OpenCV的数据类型比较.在Dlib中,图像是用二维阵列(array2d)或者矩阵(matrix)来表示的, ...