Leetcode[81]-Search for a Range
Link: https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
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].
思路:首先初始化一个2列的数组,值为-1,然后一次遍历数组。设置一个变量作为标识。记录出现target值的下标,并保存到数组中,假设标识值等于=了,就不添加它的值,保证数组第二个元素是最后一个出现target的下标。
C++:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res(2);
res[0]=-1,res[1]=-1;
int n = nums.size();
int temp = 0;
for(int i = 0; i < n; i++){
if(target == nums[i]){
if(temp == 2)
res[temp-1] = i;
else
res[temp++] = i;
}
}
if(temp ==1) {
res[temp] = res[0];
}
return res;
}
};
Leetcode[81]-Search for a Range的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [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 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- [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 (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 034 Search for a Range
题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...
随机推荐
- mysql下float类型使用一些误差详解
单精度浮点数用4字节(32bit)表示浮点数采用IEEE754标准的计算机浮点数,在内部是用二进制表示的如:7.22用32位二进制是表示不下的.所以就不精确了. mysql中float数据类型的问题总 ...
- OpenCV学习(10) 图像的腐蚀与膨胀(1)
建议大家看看网络视频教程:http://www.opencvchina.com/thread-886-1-1.html 腐蚀与膨胀都是针对灰度图的形态学操作,比如下面的一副16*16的灰度图. ...
- 教你用软碟通(UltraISO)刻录系统光盘
用光盘装系统有几个好处:1.便携,显而易见,这是最大的优点2.大容量,比之维护光盘,可以集成N多维护工具,甚至还可以放下几个ghost镜像3.维护功能强大,因为容量大,可以放更多工具.还可以设置多重启 ...
- 高性能Mysql主从架构的复制原理及配置
1. 复制概述 1.1 mysql支持的复制类型 1.2 复制解决的问题 1.3 复制如何工作 2. 2 复制配置 2.1创建复制帐号 2.2拷贝数据 2.3配置master 2.4配置slave 2 ...
- UDP套接字——(DGRAM)
/*********************程序相关信息********************* * 程序编号:014 * 程序编写起始日期:2013.11.29 * 程序编写完成日期:2013.1 ...
- [AngularJS] Angular 1.3 Anuglar hint
Read More: http://www.linkplugapp.com/a/953215 https://docs.google.com/document/d/1XXMvReO8-Awi1EZXA ...
- 出现RST的几种情况
1.端口未打开,C向S发送SYN,去连接S的端口9820,但是S没有打开9820端口,这个时候S发送RST 2.请求超时,C向S发送SYN,S回复ACK+SYN,如果C从发送SYN到收到S的ACK+S ...
- eclipse中android开发怎么修改xml文件字体大小
windows->preference->General->appearence->Colors and Font->Basic->Text Font.点击右侧的E ...
- PHP FTP函数
PHP FTP 函数 PHP FTP 简介 FTP 函数通过文件传输协议 (FTP) 提供对文件服务器的客户端访问. FTP 函数用于打开.登录以及关闭连接,同时用于上传.下载.重命名.删除及获取文件 ...
- 【转】web服务器工作原理
一.静态网页的工作原理如下:A.用户在浏览器的地址栏输入要访问的地址并回车,触发这个浏览请求. B.浏览器将请求发送到Web服务器.C.Web服务器接受这个请求,并根据请求文件的后缀名判定是否为HTM ...