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 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]
解法
二分法查找
class Solution
{
public:
vector<int> searchRange(vector<int>& nums, int target)
{
vector<int> target_pos(2, -1);
int min = 0;
int max = nums.size() - 1;
int mid = 0;
int t = 0;
while(min <= max)
{
mid = (min + max) / 2;
t = nums[mid];
if (t == target)
{
target_pos.clear();
int pre = mid - 1;
bool have = false;
while(pre >= min && nums[pre] == target)
{
have = true;
--pre;
}
if (have) target_pos.push_back(++pre);
else target_pos.push_back(mid);
have = false;
pre = mid + 1;
while (pre <= max && nums[pre] == target)
{
have = true;
++pre;
}
if (have) target_pos.push_back(--pre);
else target_pos.push_back(mid);
break;
}
else if (t > target)
{
max = mid - 1;
}
else
{
min = mid + 1;
}
}
return target_pos;
}
};
时间复杂度: O(logn).
空间复杂度: O(1).
leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array的更多相关文章
- 【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 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 刷题34. Find First and Last Position of Element in Sorted Array
一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...
- [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 == [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
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
思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...
随机推荐
- 【ASP.Net】 http请求中get,put,post,delete的区别与使用总结
在web api的设计上, 需要设计这个每个action对应的资源的请求方法是什么. Get方法是对服务器资源的请求获取, 一般get方法的参数都放在URL当中的. 所以通常情况下这种请求方式都是不安 ...
- Ubuntu 安装 matplotlib
参考: ubuntu16 安装matplotlib Ubuntu 安装 matplotlib sudo apt-get install libpng-dev libfreetype6-dev pkg- ...
- 如何在 sublime text 中以当前文件目录打开 cmd
需求描述 sublime 固定可以自己设置和添加新的编译环境,比如在我们写 js 的时候可能会添加 node 来对 js 文件进行运行.但是,这样做的结果是,我们只能看到运行结果.有时候还希望能做些其 ...
- Centos 7下添加新用户并授权
1.创建一个 xiaoyang 用户 [root@VM_81_181_centos ~]# adduser xiaoyang 2.为创建的用户设置密码 [root@VM_81_181_centos ~ ...
- 线程间操作无效: 从不是创建控件“button2”的线程访问它
在项目中经常遇到类似的问题,如何解决呢,报错的方法中添加 Control.CheckForIllegalCrossThreadCalls = false; 就可以解决了.
- Python 爬虫入门3种方法
Python 2.0 url = "http://www.baidu.com" print '第一种方法' response1 = urllib2.urlopen(url) pri ...
- C++类模板和模板类
C++ 中有一个重要特性,那就是模板类型.类似于Objective-C中的泛型.C++通过类模板来实现泛型支持. 1 基础的类模板 类模板,可以定义相同的操作,拥有不同数据类型的成员属性. 通常使用t ...
- RN酷炫组件圆形加载
地址:https://js.coach/react-native/react-native-circular-progress?search=react-native 别谢我 点个赞就行 ## Use ...
- oracle listagg和wm_concat函数
对于将一列多值合并成一行问题,oracle提供了wmsys.wm_concat和listagg函数处理此问题,下面我们以emp表中数据为例,看看两函数的使用方法 假设我们需要统计每种job下面有哪些员 ...
- 使用JDBC从数据库中查询数据的方法
* ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...