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) 分 ...
随机推荐
- python使用ctypes模块下的windll.LoadLibrary报OSError: [WinError 193] % 不是有效的 Win32 应用程序
原因:python是64位的python,而windll.LoadLibrary只能由32位的python使用 参考: 64位Python调用32位DLL方法(一) 解决方法:使用32位的python ...
- BootStrapValidate 简单使用
前阵子用了bootstrapvalidate写了一个登录验证,这里小记一笔 首先需要引入 bootstrapValidator.css //可不引入 jquery-2.1.0.min.js boots ...
- python的subprocess基本
先在同一个文件夹下创建两个.py文件. 第一个:13.py # -*- coding: utf-8 -*- __author__ = "YuDian" ''' multiproce ...
- 20155322 2016-2017-2 《Java程序设计》实验一 Java开发环境的熟悉(macOS + Eclipse)
20155322 2016-2017-2 <Java程序设计>实验一 Java开发环境的熟悉(macOS + Eclipse) 实验目的与内容 熟悉命令行开发环境. 使用vim等文本编译器 ...
- 前后端分离之JWT用户认证zf
在前后端分离开发时为什么需要用户认证呢?原因是由于HTTP协定是不储存状态的(stateless),这意味着当我们透过帐号密码验证一个使用者时,当下一个request请求时它就把刚刚的资料忘了.于是我 ...
- 请求头(request headers)和响应头(response headers)解析
*****************请求头(request headers)***************** POST /user/signin HTTP/1.1 --请求方式 文件名 http ...
- Luogu1445 [Violet]樱花
题面 题解 $$ \frac 1x + \frac 1y = \frac 1{n!} \\ \frac{x+y}{xy}=\frac 1{n!} \\ xy=n!(x+y) \\ xy-n!(x+y) ...
- angular promise $q 异步调用
Angular异步调用 Promise和$q的用法 背景 首先说明一下promise异步调用出现的背景: javascript语言是一种单线程模式,就是说一次只能够执行一个任务,如果有多个任务的话就必 ...
- My status
I haven‘t any one who is strong relationship with me. My skill is normal. I'm not interesting in neg ...
- shell 参数
转:http://hi.baidu.com/ipvsadm/item/489d9e16460195ddbe9042ee linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中s ...