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]
------------------------------------------------------------------------------------
这个题是在leetcode和lintcode 上都有的,只不过题目里面给的参数有些是不同的,需要注意的,不过代码是基本相同的。
用遍历数组的方式可能会TLE,所以用二分更好。
emmm,二分查找也可以用递归方式来写的。当用二分查找方式得到一个不为-1数组下标时,向两边扩展,最终得到一个范围。
C++代码:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int ans = search(nums,,nums.size()-,target);
if(ans == -) return {-,-}; //归结根底,vector就是一个数组,可以用{}。
int left = ans,right = ans;
while(left > && nums[left-] == nums[ans]) left--;
while(right < nums.size() - && nums[right+] == nums[ans]) right++;
return {left,right};
}
int search(vector<int>& nums,int left,int right,int target){
if(left > right) return -;
int mid = left + (right - left)/;
if(nums[mid] == target) return mid;
if(nums[mid] > target) return search(nums,,mid-,target);
else return search(nums,mid+,right,target);
}
};

还有一个解法:

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int ans = search(nums,,nums.size()-,target);
if(ans == -) return {-,-};
int left = ans,right = ans;
while(left > && nums[left-] == nums[ans]) left--;
while(right < nums.size() - && nums[right+] == nums[ans]) right++;
return {left,right};
}
int search(vector<int>& nums,int left,int right,int target){
// if(left > right) return -1;
// int mid = left + (right - left)/2;
// if(nums[mid] == target) return mid;
// if(nums[mid] > target) return search(nums,0,mid-1,target);
// else return search(nums,mid+1,right,target);
if(nums.size() == ) return -;
while(left + < right){
int mid = left + (right - left)/;
if(nums[mid] > target) right = mid;
else if(nums[mid] < target) left = mid;
else return mid;
}
if(nums[left] == target) return left;
if(nums[right] == target) return right;
return -;
}
};

emmm,也可以看官方题解:https://leetcode.com/articles/find-first-and-last-position-element-sorted-array/

(二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range的更多相关文章

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

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

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

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

  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

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  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. 34. Find First and Last Position of Element in Sorted Array + 二分

    题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...

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

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

  9. leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array

    思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...

随机推荐

  1. Linux中VSFTP的配置

    配置VSFTP服务器: 1.安装VSFTP,可以参考Linux 中yum的配置来安装: yum installvsftpd.x86_64 -y 2.修改SELinux: setenforce 0 查看 ...

  2. java.lang.NullPointerException一个低级的解决方法

    java.lang.NullPointerException 这次因为调用了类的方法的时候忘记了new对象了 导致该对象为空

  3. 有效的字母异位词的golang实现

    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 输入: s = "anagram", t = "nagaram" 输出: ...

  4. ORACLESQL语句的优化

    ORACLESQL语句的优化: 选择最有效率的表名顺序:应该选择记录条数最少的表作为基表! 因为Oracle解析器的处理顺序是从右至左的.当ORACLE处理多个表时, 会运用排序及合并的方式连接它们. ...

  5. ubuntu创建idea桌面快捷方式

    This method can be used to create a launcher for any application, not just IntelliJ IDEA. For any la ...

  6. Vue-Router模式、钩子

    转:https://www.cnblogs.com/heioray/p/7193841.html 模式 vue-router中的模式选项主要在router实例化的时候进行定义的,如下 const ro ...

  7. linux目录1

    1.linux常用快捷键 2.linux命令之vmstat 3.linux命令之df 4.linux命令之netstat 5.linux命令之 tar 6.The authenticity of ho ...

  8. 多节点,多线程下发订单,使用zookeeper分布式锁机制保证订单正确接入oms系统

    假设订单下发, 采用单机每分钟从订单OrderEntry接口表中抓100单, 接入订单oms系统中. 由于双十一期间, 订单量激增, 导致订单单机每分钟100单造成, 订单积压. 所以采用多节点多线程 ...

  9. SpringMVC项目读取不到外部CSS文件的解决办法及总结

    昨天,在写一个新需求的时候,因为没有前端同事的对接,无奈只有自己写css,js放到ssm结构的后台项目中,因为之前开发都是把前端文件放在一个专门的服务器上,一直没有关注服务端项目读取静态资源的问题,运 ...

  10. 安装Laravel框架,利用composer

    学一学PHP框架--Laravel的设计思想. 先安装Laravel: Laravel的文档很全:参考 http://www.golaravel.com/ 既然文档很全,就简单说下几个重点.以下以安装 ...