**一. 题目描述**
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(logn)`.
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`,在这个序列中寻找等于`target`的**元素的下标范围**。由于序列已经排好序,直接用二分查找,分别求等于`target`的最靠左的元素下标`left`和最靠右的元素下标`right`即可。
**三. 示例代码**
~~~
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int n = nums.size();
int left = searchRangeIndex(nums, target, 0, n - 1, true);
int right = searchRangeIndex(nums, target, 0, n - 1, false);
vector<int> result;
result.push_back(left);
result.push_back(right);
return result;
}
private:
int searchRangeIndex(vector<int>& nums, int target, int low, int high, bool isLeft)
{
while (low <= high)
{
int midIndex = (low + high) >> 1;
if (nums[midIndex] == target)
{
int temp = -1;
if (isLeft)
{
if (nums[midIndex] == nums[midIndex - 1] && low < midIndex)
temp = searchRangeIndex(nums, target, low, midIndex - 1, true);
}
else
{
if (nums[midIndex] == nums[midIndex + 1] && high > midIndex)
temp = searchRangeIndex(nums, target, midIndex + 1, high, false);
}
return temp == -1 ? midIndex : temp; // temp == -1时表示只有中间一个值等于target
}
else if (nums[midIndex] > target)
high = midIndex - 1;
else
low = midIndex + 1;
}
return -1; // 找不到target,输出-1
}
};
~~~
![](https://box.kancloud.cn/2016-01-05_568bb5f1c162b.jpg)
**四. 小结**
注意题目要求`O(logn)`的时间复杂度,算法写的不好可能会超时。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching