leetcode第33题--Search for a Range
Given a sorted array of integers, 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].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
给的那个一个有序数组和一个目标target,找到这个目标在有序数组里的开始和结束的下标。如果不存在就返回 -1 -1。注,如果数组中目标值只有一个,那就返回两个相同的下标。
用binary search找到target 然后再在左边binary search找到左边(包括mid)第一个目标,记录下标。再在右边binary 找到最后一个目标。记录下标。返回。
代码如下:
class Solution {
public:
vector<int> searchRange(int A[], int n, int target)
{
vector<int> wr, ans;
wr.push_back(-1); // 要学会可以用 vector<int> wr(2,-1);直接将其初始化为想要的
wr.push_back(-1);
if(n == 0)
return wr;
int l = 0, r = n -1, mid = 0;
while(l <= r)
{
mid = (l+r)/2;
if (A[mid] < target) // 如果mid小于target那么可以把左边一块去掉
{
l = mid + 1;
continue;
}
if (A[mid] > target) // 如果mid大于target那么可以把右边一块去掉
{
r = mid -1;
continue;
}
if (A[mid] == target)// mid 找到了刚好,那就肯定在这里面要有return,如果一直没有找到,跳出while后就返回wr
{
// find start from l to mid,binary search
int start = l, l_r = mid; // l_r指mid左边的最右
while(l <= l_r)
{
int tmp_mid = (l + l_r)/2;
if(A[tmp_mid] != target)
{l = tmp_mid + 1;}
if(A[tmp_mid] == target)
{l_r = tmp_mid - 1; start = tmp_mid;}
}
ans.push_back(start);
// find end from mid to r,binary search
int r_l = mid, End = mid; // r_l指mid右边的最左
while(r_l <= r)
{
int tmp_mid = (r_l + r)/2;
if(A[tmp_mid] != target)
{r = tmp_mid - 1;}
if(A[tmp_mid] == target)
{r_l = tmp_mid + 1; End = tmp_mid;}
}
ans.push_back(End);
return ans;
}
}
return wr;
}
};
leetcode第33题--Search for a Range的更多相关文章
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- LeetCode第[33]题(Java):Search in Rotated Sorted Array
题目:在翻转有序中搜索 难度:Medium 题目内容: Suppose an array sorted in ascending order is rotated at some pivot unkn ...
- leetcode第32题--Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode(33)Search in Rotated Sorted Array
题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...
- 【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode 34. 搜索范围(search for a range)
题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值 ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
随机推荐
- Android开发技巧——实现在图标文本底部导航栏(更新)
本文参考了导航栏的代码viewpagerindicator实现. 本文介绍了之前版本号qq或者微信,添加文本,实现图标,导航栏的底部. 2014-09-14 13:59:42更新:library的代码 ...
- 关于int全区变量读写的原子性
关于int全区变量读写的原子性 关于int变量的读写是否原子性网上有非常多讨论,貌似不同平台不同,这里自己做实如今arm9平台測试.这里要注意原子性并不是指一条汇编才原子,实际上即使一次赋值编 ...
- 理解Android虚拟机体系结构(转)
1 什么是Dalvik虚拟机 Dalvik是Google公司自己设计用于Android平台的Java虚拟机,它是Android平台的重要组成部分,支持dex格式(Dalvik Executable)的 ...
- OCP-1Z0-051-标题决心-文章2称号
2. View the Exhibit to examine the description for the SALES table. Which views can have all DML ope ...
- [转]从.NET转JAVA开发
[转]从.NET转JAVA开发 .NET转JAVA其实也很简单,自己动手写几个DEMO差不多就了解了 1. JAVA做互联网开发多半只会用到开源框架Struts2 Hibernate 和Spring, ...
- bzoj 2437 [Noi2011]兔子和鸡蛋 [二分图匹配]
叙述性说明 这些日子.兔子和蛋像一个新的棋盘游戏. 这场比赛是在 n 行 m 在船上进行列. 前,棋盘上有一 个格子是空的,其他的格子中都放置了一枚棋子,棋子或者是黑色,或者是白色. 每一局游戏总是兔 ...
- leetcode第一刷_Length of Last Word
不是非常明确出题人的意图,事实上这道题用java的话简直是太简单了,用split处理一下,得到全部单词的一个数组,然后求最后一个的长度即可了.我个人认为java里最成功的函数就是split了,我做pr ...
- Cocos2d-x 脚本语言Lua使用
Cocos2d-x 脚本语言Lua使用 前面几篇博客已经把Lua的相关基础知识介绍了.本篇博客就来介绍一下,怎样在Cocos2d-x项目中使用Lua这门脚本语言进行开发.因为笔者使用的时Mac系统.所 ...
- 编程算法 - 数丑陋 代码(C)
数丑陋 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 我们把仅仅包括因子2, 3 和 5的数称作丑数. 求按从小到大的顺序的第5个丑数. 能够 ...
- Codeforces Round #277.5 (Div. 2)A——SwapSort
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...