题目:

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].  (Medium)

分析:

标准的二分搜索题,找一个target的范围,也就是first position 元素等于target 和last position元素等于。

写两次二分搜索,注意中间start,end的变化情况的不同,一个为了保留住第一个满足条件的,一个为了保留住最后一个满足条件的。

代码:

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result;
if (nums.size() == ) {
return result;
}
int start = , end = nums.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (nums[mid] < target) {
start = mid;
}
else {
end = mid;
}
}
if (nums[start] == target) {
result.push_back(start);
}
else if (nums[end] == target) {
result.push_back(end);
} start = ;
end = nums.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (nums[mid] <= target) {
start = mid;
}
else {
end = mid;
}
}
if (nums[end] == target) {
result.push_back(end);
}
else if (nums[start] == target) {
result.push_back(start);
}
if (result.size() != ) {
return result;
}
else {
return vector<int> {-, -};
}
}
};
 

LeetCode34 Search for a Range的更多相关文章

  1. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

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

  3. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

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

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

  5. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

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

  7. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

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

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

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

随机推荐

  1. 【转】Java transient关键字

    Volatile修饰的成员变量在每次被线程访问时,都强迫从主内存中重读该成员变量的值.而且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在任何时刻,两个不同的线程总是看到某个成员变量的同一 ...

  2. Python装饰器(decorator)

    了解装饰器,要先了解闭包. 1,闭包(closure) 闭包是Python所支持的一种特性,它让在非global scope定义的函数可以引用其外围空间中的变量,这些外围空间中被引用的变量叫做这个函数 ...

  3. windows server 2008 支持 .net framework 4.0

    windows server 2008平台下需要安装sp1,或打KB958854补丁,IIS7.0才能支持.net framework 4.0. 否则,IIS7.0中的应用程序虽然被配置为.net 4 ...

  4. JS:公历、农历互转

    先申明这段代码不是我写的,纯粹只是觉的比较好用,所以记录下来以后继续使用,也同样分享给大家,大家有更好的可以推荐给我,谢谢! function CalConv(M, dateStr) { if (da ...

  5. 关于request.getsession(true|false)

    request.getSession(true):若存在会话则返回该会话,否则新建一个会话.request.getSession(false):若存在会话则返回该会话,否则返回NULL

  6. Find n‘th number in a number system with only 3 and 4

    这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...

  7. 用jQuery解决弹出层的问题

    在BS 项目中 经常需要用到这种弹出层.做这种弹出层一般都会遇到下面几个问题:0,弹出层必须定义在input的下边显示.1,点击input弹出div层.2,点击div层外面任何地方,关闭div层.3, ...

  8. J2534 Pass-Thru Vehicle Programming ( SAE J1962 connector and Protocol )

    SAE J1962—Diagnostic Connector SAE J1850—Class B Data Communications Network Interface SAE J1939—Tru ...

  9. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  10. hdu 4597 Play Game 区间dp

    Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...