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的更多相关文章

  1. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  2. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. [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 ...

  5. 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 ...

  6. [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. 思路 ...

  7. 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 ...

  8. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  9. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

随机推荐

  1. [Android Studio] Android Studio快速定位当前打开的文件在哪个目录(package)下

    转载自:http://blog.csdn.net/hyr83960944/article/details/38067499 在Eclipse中有一个很好的功能,就是比如我打开一个AActivity,左 ...

  2. 如何在jenkins上通过mvn方式运行sonar

    1.首先在jenkins所在机器的的maven配置文件(settings.xml)里做如下配置: <profile> <id>sonar</id> <acti ...

  3. Linux环境MySQL集群配置

    一.介绍 ======== 这篇文档旨在介绍如何安装配置基于2台服务器的MySQL集群.并且实现任意一台服务器出现问题或宕机时MySQL依然能够继续运行. 注意! 虽 然这是基于2台服务器的MySQL ...

  4. iOS:shareSDK第三方登录

    shareSDK第三方登录跟分享差不多,比较简单,前面已有介绍.这里简单写一下第三方登录吧. 1.首先:我用到了QQ.微信.新浪这三个平台的登录,需要到它们各自的开发者平台注册开发者账号,这是我的QQ ...

  5. OpenMP 线程同步之临界区

    多核/多线程编程中肯定会用到同步互斥操作.除了互斥变量以为,就是临界区. 临界区是指在用一时刻只允许一个线程执行的一段用{...},包围的代码段. 在OpenMP中临界区声明方法如下: #pragma ...

  6. Project has no project.properties file! Edit the project properties to set one.

    解决办法: 右击项目,选择android tools-->fix project properties.然后重启eclipse即可.

  7. bitBucket readme文件图片添加

    bitBucket一个和github一样的强大的代码托管站点,前者支持免费无限的私有仓库:后者私有仓库要付费: 在bitbucket项目中可以使用markDown语法创建一个README.md文件,但 ...

  8. PHP Filter函数

    PHP Filter 函数 PHP Filter 简介 PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤. 安装 Filter 函数是 PHP 核心的组成部分.无需安装即可使用这 ...

  9. Codeforces Round #FF (Div. 1)-A,B,C

    A:DZY Loves Sequences 一開始看错题了. .sad. 题目非常easy.做法也非常easy.DP一下就好了. dp[i][0]:到当前位置,没有不论什么数改变,得到的长度. dp[ ...

  10. JUnit 3.8 让所有测试程序 实现 复合的测试(TestSuite)

    之前是单个单个程序测试,这种方式在测试类比较少的时候可行, 但测试类多了,单个单个的这个测试方式就不推荐了,那得使用 复合的测试了 一个TestSuite是一个复合的测试.它运行测试用例集.   这个 ...