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 ...
随机推荐
- 第一章 在linux下python读串口 存MYSQL数据库(703N)
import MySQLdb//定义引用数据库的驱动文件 import serial import time ser = serial.Serial('/dev/ttyATH0', 115200, t ...
- 《Android传感器高级编程》
<Android传感器高级编程> 基本信息 原书名:Professional Android Sensor Programming 原出版社: Wrox 作者: (美)米内特(Greg M ...
- Power Desginer系列00【转载】
绪论 Sybase PowerDesigner(简称PD)是最强大的数据库建模工具,市场占有率第一,功能也确实十分强大,现在最新版本是15.1,已经支持最新的SQL Server 2008等数据库,另 ...
- input type=file文件选择表单元素二三事
一.原生input上传与表单form元素 如果想使用浏览器原生特性实现文件上传(如图片)效果,父级的form元素有个东西不能丢,就是: enctype="multipart/form-dat ...
- 修改ubuntu下网卡名不是eth0的问题
sudo nano /etc/default/grub 找到GRUB_CMDLINE_LINUX=""改为GRUB_CMDLINE_LINUX="net.ifnames= ...
- idea maven项目模块中的Content Root
- [Grunt] Watch && grunt-contrib-watch
Watch is an essential component of any Grunt build, and you will find yourself using it in almost ev ...
- [原创-性能调优]借助AWR报告分析解决oracleCPU过高的问题
简介:在oracle数据库中,有两个非常实用的自带监控工具EM(Enterprise Manager)和AWR(Automatic Workload Repository).其中,通过AWR报告可以生 ...
- Python continue
continue继续循环在循环过程中,可以用break退出当前循环,还可以用continue跳过后续循环代码,继续下一次循环.假设我们已经写好了利用for循环计算平均分的代码: L = [75, 98 ...
- Git-学习笔记(常用命令集合)
这里引用一下百度百科里Git的内容: Git --- The stupid content tracker, 傻瓜内容跟踪器.Linus Torvalds 是这样给我们介绍 Git 的. Git 是用 ...