描述:

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. Android——点击对话框上button不关闭对话框

    有时候我没可能须要在点击button进行一些检測.可是并不想关闭次对话框(系统默认点击不论什么一个button则关闭对话框),处理方法例如以下:在点击事件下加入例如以下代码: try { Field  ...

  2. Shiro学习小结

    1. What is Shiro? Apache旗下一个开源的Java权限框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架 ...

  3. SubVersion(SVN)的安装配置使用

    一. SubVersion服务器端安装 安装软件:Setup-Subversion-1.6.4.msi,下载地址:http://subversion.tigris.org/servlets/Proje ...

  4. ios 从url字符串中获取图片名字

    NSString *str = @"http://pic92.nipic.com/file/20160323/22486259_160209631000_2.jpg"; NSLog ...

  5. IQueryable和IEnumerable以及AsEnumerable()和ToList()的区别

    注意:本文背景为 Linq to sql .文中ie指代IEnumerable,iq指代IQueryable. IQueryable 和 IEnumerable 的区别 IQueryable 延时执行 ...

  6. 解决Incorrect integer value: &#39;&#39; for column &#39;id&#39; at row 1的方法

    在使用Navicat for MySQL还原数据库备份时.出现Incorrect integer value: '' for column 'id' at row 1的错误; 网上查资料发现5以上的版 ...

  7. ROS多个网段做隔离

    ROS路由器有多个LAN口,网段分别是 192.168.0.0/24 和 192.168.1.0/24. 用192.168.1.0/24做为访客使用,因此在路由器上面设置过滤器. /ip firewa ...

  8. YUV420视频上面添加字幕

    1.source_codemain.c中实现了函数draw_Font_Func(),这个函数可以直接移植到C程序中使用.zimo.h里面放的是字模转码后的数据. 2.data_yuv测试用的yuv42 ...

  9. Java中的equals方法和自定义比较器

    Object中的equals()方法默认是按地址比较,而不按内容进行比较, public boolean equals(Object obj) { return (this == obj); } 在S ...

  10. freemarker在xml文件中遍历list数据

    delete   from pub_channelpackage   where channelcode = :channelcode   and channeltype = :channeltype ...