Leetcode 34
//二分查找后,向两边扩展,二分错了两次,现在是对的。
//还有就是vector可以用{}直接赋值很棒 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int left = ;
int right = nums.size()-;
int mid = -;
bool flag = false;
while(left<=right){
mid = (left+right)/;
if(nums[mid] == target){flag = true;break;}
else if(nums[mid] < target){left = mid+;}
else{right = mid-;}
}
if(flag == false) return {-,-}; int begin = mid;
while(begin >= ){
if(nums[begin] != target) break;
begin--;
}
begin++; int end = mid;
while(end < nums.size()){
if(nums[end] != target)break;
end++;
}
end--;
return {begin,end};
}
};
//递归的二分
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;
else if (nums[mid] < target) return search(nums, mid + , right, target);
else return search(nums, left, mid - , target);
}
Leetcode 34的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- [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. 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 ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- (二分查找 拓展) 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 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- Java实现 LeetCode 34 在排序数组中查找元素的第一个和最后一个位置
在排序数组中查找元素的第一个和最后一个位置 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
随机推荐
- python3.7.2 ssl版本过低导致pip无法使用的问题
环境:系统是centos6.6,python:python3.7.2 问题:安装好python3.pip后,在通过pip install xx 安装模块时,发现无法安装的问题,提示版本太低,系统默认的 ...
- Delphi中那些容易混淆的基础(@、^、Addr、Pointer,Move、CopyMemory,GetMem和FreeMem、GetMemory和FreeMemory、New和Dispose、StrAlloc和StrDispose、AllocMem)
@.^.Addr.Pointer Delphi(Pascal)中有几个特殊的符号,如@.^等,弄清楚这些符号的运行,首先要明白Delphi指针的一些基础知识:指针,是一个无符号整数(unsigned ...
- Python多进程编程(转)
原文:http://www.cnblogs.com/kaituorensheng/p/4445418.html 阅读目录 1. Process 2. Lock 3. Semaphore 4. Even ...
- SQL调优学习之——sqlserver分页从低效到高效
背景 首先感谢网友@aixuexi 在评论中的提醒,原博文介绍的几种都不是最高效,现已修改加入另一种更高效的方法. 以前都是使用mysql和oracle,对sqlserver的使用不多.最近因项目原因 ...
- sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误。
sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误. 错误情况,如下图: 原因分析:硬盘故障 解决办法:进行迁移
- Linux查看系统版本信息
1.查看操作系统相关信息 uname -a 2.查看正在运行的内核版本信息 cat /proc/version //uname -r 3.查看发行版本号 (适用于所有的linux,包括Redhat. ...
- MISC-WHCTF2016-crypto100
题目:李二狗的梦中情人 找不同! 如图,下载得到“nvshen.png” 流程:看到这个被命名为nvshen的文件,感觉文件本身会有东西.用16进制查看器在图片的末尾发现了一串类似URL的ASCII ...
- 使用spring cache和ehcache之前必须了解的
好长时间没写博客了,真的是没时间啊.ps:其实就是懒!!接下来几篇要写下缓存,这里主要写下ehcache与spring整合的内容,包括aop形式的缓存,基于注解的缓存,页面缓存这三方面吧.在这之前先要 ...
- 两步实现在Git Bash中用Sublime打开文件
每次都要用鼠标点来点去才能用sublime打开文件!太不科学!今天来配置一下在Git bash中用sublime打开文件 方法 新建一个文件命名为你想要的命令,比如 subl(注意不能有后缀名),内容 ...
- 期末总结20135320赵瀚青LINUX内核分析与设计期末总结
赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 对LINUX内核分析与设计这 ...