描述:

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]

思路一:一次Binary Search

先使用二分查找找到和taget相等的起始位置的元素,然后在这个位置向两边扩散找到值相同的范围。

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res(,-);
if(nums.size() == ) return res;
int cau = dichotomy(nums,target);
if(cau == -) return res;
else{
int i = cau,j = cau;
cout<<cau<<endl;
while((i>= && nums[i] == target) || (j<=nums.size()- && nums[j] == target)){
if(i>= && nums[i] == target){
res[] = i;
cout<<i<<endl;
i--;
}
if(j<=nums.size()- && nums[j] == target){
res[] = j;
cout<<j<<endl;
j++;
}
}
}
return res;
} int dichotomy(vector<int>& nums, int target){
int low = ,int high = nums.size() - ;
while(low < high){
int mid = (low + high + ) / ;
if(nums[mid] == target) return mid;
if(nums[mid] < target) low = mid + ;
else high = mid - ;
}
return -;
}
};

思路二:两3次Binary Search

先根据上述的二分查找,找到起始位置的元素,然后从low开始继续使用一次二分查找找到target+1的位置,然后返回这个位置it - 1,从而找到起始和终止的范围。

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res(,-);
if(nums.size() == ) return res;
int low = dichotomy(nums,target,); //找起始元素
cout<<low<<endl;
if(low == nums.size() || nums[low] != target) //可能出现到数组结尾还是target比low处元素大,low=nums.size
return res;
else{
res[] = low;
res[] = dichotomy(nums,target+,low) - ; //找结尾元素
}
return res;
} int dichotomy(vector<int>& nums, int target, int low){
int high = nums.size(); //核心
while(low < high){
int mid = (low + high ) / ;
if(nums[mid] < target) low = mid + ;
else high = mid;
}
return low;
}
};

【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array的更多相关文章

  1. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

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

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

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

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

  4. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  5. Find First and Last Position of Element in Sorted Array - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...

  6. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  7. leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array

    leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array Given an array of int ...

  8. Leetcode: Find First and Last Position of Element in Sorted Array

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

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

随机推荐

  1. JavaScript 获取文件名,后缀名

    function getBaseName(str) { var segs = str.split('.'); if(segs.length > 1) segs.pop(); return seg ...

  2. matplotlib表面三维图

    1.basic numpy.meshgrid 由一维数组到二维数组,用于生成网格数据 matplotlib python绘图库 2.code In [88]: from mpl_toolkits.mp ...

  3. 详述Centos中的ftp命令的使用方法

    ftp服务器在网上较为常见,Linux ftp命令的功能是用命令的方式来控制在本地机和远程机之间传送文件,这里详细介绍Linux ftp命令的一些经常使用的命令,相信掌握了这些使用Linux 进行ft ...

  4. maven设置本地仓库地址和设置国内镜像

    <?xml version="1.0" encoding="UTF-8"?> <!-- 英文注释已经被删除了,直接修改本地仓库地址用就行了. ...

  5. CentOS 下Mysql数据库的安装与配置

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常 的方便,在Linux上如果要安装数据库, ...

  6. C的字符串操作接口实现

    近期在写一个关于用HTTP控制storm的的UI功能.已经实现完毕,採用在nginx里面增加相应的模块调用来实现,模块中调用一个动态载入的SO.这个SO用THRIFT和zookeeper client ...

  7. eclipse编写scala应用运行在spark集群上

    代码 package spark_1 import org.apache.spark.SparkConf import org.apache.spark.SparkContext class Work ...

  8. TCP粘包处理通用框架--C代码

    说明:该文紧接上篇博文“ linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现 ”讲来 (1)TCP粘包处理数据结构设计 #define MAX_MSG_LEN 65535 ty ...

  9. oracle中避免sort操作

    1.升序排列 create index ix_table1 on table1 (column1,column2); select column1,column2 from table1 order ...

  10. c语言中external,static关键字用法

    static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...