【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 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的更多相关文章
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_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] 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 ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- 刷题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-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 ...
- 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 ...
- [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 ...
随机推荐
- 51单片机 | 并行I/O口扩展实例(74LS244/74LS373/4071)
并行I/O口扩展实例 //<51单片机原理及应用(第二版)——基于Keil C与Proteus>第四章例4.4 I/O口不能完全用于输入/输出操作,当需要扩展外部存储器时,P0.P2口用作 ...
- 安装IntelliJ IDEA默认C盘文件过大怎么办
方法如下: 找到安装路径下有个属性文件,我的是在 D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.2\bin 进入bin目录后找到属性文件:idea.p ...
- Eclipse配色利器
1 http://eclipsecolorthemes.org/ 这是官网 2 安装后,window-preferences-general-appearance-color theme 即可找到多 ...
- 深入探析 Rational AppScan Standard Edition 新特性之 Glass Box 扫描
众所周知,Web 应用安全测试通常有黑盒安全测试和白盒安全测试两种方法.这两种方法孰优孰劣一直众议纷纷.广为公认的是,这两种测试方法有着良好地互补性,两种测试方法的结合是未来安全测试技术的发展趋势.G ...
- Linux Mint (应用软件— 虚拟机:Virtualbox)
近期想自己折腾一下Linux系统本身.比方Linux裁减或者移植.裁减或者移植Linux是一件麻烦的事情.而且出错后会影响到当前的系统.怎样才干不影响当前机器上的系统呢,于是便想到了虚拟机.在当前系统 ...
- 计算机网络11--OSI參考模型
本页内容 1.OSI參考模型简单介绍 2.OSI參考模型通信过程 3.OSI參考模型的数据封装 4.数据封装的意义 5.物理层 6.数据链路层 7.网络层 8.传输层 9.会话层 10.表示层 11. ...
- css 温故而知新 select-option 文字方向居右
对select-option使用text-align:right;是无效的. 正确的姿势是:direction: ltr; 另外值得一提的是,通常还需要配合一点padding来美化.
- css写箭头
/* 向上的箭头 */ .dot-top { font-size: 0; line-height: 0; border-width: 10px; border-color: red; border-t ...
- 解析spark RDD
RDD是spark抽象的基石,可以说整个spark编程就是对RDD进行的操作 RDD是弹性的分布式数据集,它是只读的,可分区的,这个数据集的全部或者部分数据可以缓存在内存中,在多次计算间重用.所谓 ...
- [Java]事件驱动程序设计
事件驱动模型三大要素 1)事件源:能接收外部事件的源体: 2)监听器xListener:能接收事件源通知的对象: 3)处理器Handler:用于处理事件的对象. 在Java中使用监听器对象处理事件的方 ...