leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array
思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界。
class Solution {
public:
int begin = -, end = -;
int ends;
int lSearch(int left, int right, vector<int>& nums, int target)
{
if(left > right) return -;
int mid = (left + right) / ;
if(nums[mid] == target){
if(mid == || (mid > && nums[mid - ] < target)) return mid;
else return lSearch(left,mid - ,nums,target);
}else
return lSearch(mid + ,right,nums,target);
return -;
}
int rSearch(int left, int right, vector<int>& nums, int target)
{
if(left > right) return -;
int mid = (left + right) / ;
if(nums[mid] == target){
if(mid == ends || (mid < ends && nums[mid + ] > target)) return mid;
else return rSearch(mid + ,right,nums,target);
}else
return rSearch(left ,mid - ,nums,target);
return -;
}
int midSearch(int left, int right, vector<int>& nums, int target)
{
if(left > right) return -;
int mid = (left + right) / ;
if(nums[mid] == target){
return mid;
}
else if(nums[mid] < target) return midSearch(mid + ,right,nums,target);
else if(nums[mid] > target) return midSearch(left, mid - ,nums,target);
return -;
}
vector<int> searchRange(vector<int>& nums, int target) {
ends = nums.size() - ;
int mid = midSearch(,ends,nums,target);
if(mid != -){
begin = lSearch(,mid,nums,target);
end = rSearch(mid,ends,nums,target);
}
vector<int> ans;
ans.push_back(begin);
ans.push_back(end);
return ans;
}
};
leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array的更多相关文章
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 刷题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 && 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- [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
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- 34. Find First and Last Position of Element in Sorted Array + 二分
题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...
随机推荐
- SharePoint2010代码启动工作流
1. private void StartWorkFlow() { //获得该列表上的发布的所有工作流 SPWorkflowAssociationCollection wfAssociationCol ...
- iOS 数据安全、数据加密传输
近期接到一个新需求:APP企业版需要接入热更新功能. 热更新需要下发补丁脚本, 脚本下发过程中需要保证脚本传输安全,且需要避免中间人攻击. 需要用到数据加密传输方面的知识,以下是我设计的加密解密流程: ...
- 设计一个学生类&班级类
第一题 设计一个学生类 属性:姓名.学号.年龄.成绩 设计一个班级类要求:实现向班级中添加学生.删除学生.查看学生.按照指定条件排序 属性:班级代号,所有学生 需要使用 calss, __int__, ...
- 学习ThinkPHP5的第一天(安装 连接数据库)
参考文档:thinkPHP5.0完全手册 一.安装 采用的是git安装方式: 应用项目:https://github.com/top-think/think 核心框架:https://github. ...
- 一起来学大数据——走进Linux之门,学习大数据的重中之重
昨天我们看了有关大数据Hadoop的一些知识点,但是要在学习大数据之前,我们还是要为大数据的环境做一些的部署. 那么,今天我们就来讲讲开启我们大数据之路的Linux,跟上我们的脚步yo~ Linux介 ...
- [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)
建议参考文档: S5PV210-iROM-ApplicationNote-Preliminary-20091126 S5PV210_UM_REV1.1 项目介绍参考 [project X] tiny2 ...
- 浅析Vue.js 中的条件渲染指令
1 应用于单个元素 Vue.js 中的条件渲染指令可以根据表达式的值,来决定在 DOM 中是渲染还是销毁元素或组件. html: <div id="app"> < ...
- [Golang学习笔记] 05 程序实体2 作用域访问权限和变量重声明
作用域访问权限: 程序实体访问权限(作用域)有三种:1. 包级私有(代码包)2. 模块级私有(代码包)3. 公开(全域). 一个函数是一个代码块.一个程序实体的作用域总是会被限制在某个代码块中.好处: ...
- Java设计模式(24)——行为模式之解释器模式(Interpreter)
一.概述 概念 自己定义文法,实际中还是很少出现的,作了解 给出一篇网友的参考博文:http://blog.csdn.net/ylchou/article/details/7594135
- 2017-2018-1 《信息安全技术》实验二——Windows口令破解
2017-2018-1 <信息安全技术>实验二--Windows口令破解 所用工具 系统:能勾起我回忆的Windows 2003 工具:LC5.SuperDic Windows口令破解 口 ...