一、题目说明

题目是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的更多相关文章

  1. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  2. [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 ...

  3. (二分查找 拓展) 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 ...

  4. [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 ...

  5. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  6. [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 ...

  7. 【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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. iOS 13适配

    1. 安装时,加入Xcode11.3 后 原xcode会安装开发工具插件时候出现 点击安装插件之后会出现 目前没找到解决方案.只能在一个mac电脑上安装使用一个版本. 2.编译时,会出现libstdc ...

  2. Tomcat JDK MySql 安装配置

    Tomcat 7绿色版指定jdk并注册服务  https://blog.csdn.net/weixin_43976019/article/details/89386171   例如:service.b ...

  3. 【LeetCode 】N皇后II

    [问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...

  4. hdu 3388 Coprime

    第一个容斥的题,感觉这东西好神啊.于是扒了一发题解2333 首先想对于[1,x]内有多少与n,m都互质的数,显然x是存在单调性的,所以可以二分一下. 那么互质的数的求法,就是x-存在n,m一个质因数的 ...

  5. CF_448D 二分

    给定n m k n和m为一个矩阵的行和列,都从1开始,矩阵的每个元素的值即为 i*j(行*列),求里面第k个数 还想找什么规律,发现虽然矩阵里面很有规律,但是n 和m在不断变化 根本不好找 其实元素从 ...

  6. spring boot 环境配置(profile)切换

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. Tensorflow学习教程------softmax简单介绍

    做机器学习的同志们应该对这个都不陌生,这里简单举个例子.一般来说,使用softmax函数来将神经元输出的数值映射到0到1之间,这样神经元输出的值就变为了一个概率值. 公式和例子如下图 公式和例子如下图

  8. Centos7 死循环登录问题

    问题:用户名和密码输入正确,登录后屏幕闪一下又回到初始的登录界面.不知道具体什么原因引起的,先记录下不知道是否正确的解决方案,网上找了些相关的方案有的也实现不了,可能这个问题跟装的虚拟机的版本也有关系 ...

  9. 应用层上的协议HTTP

    HTTP http://www.runoob.com/http/http-tutorial.html https://www.cnblogs.com/houfee/articles/9161847.h ...

  10. 201609-2 火车购票 Java

    思路待补充 import java.util.Scanner; class Main{ public static void main(String[] args) { //100个座位 int[] ...