Search for a Range

原创 2015年11月18日 14:19:45

题目来源:https://leetcode.com/problems/search-for-a-range/

class Solution {
public:
    // 记住最后递归的left 和right 能够减少寻找左边界和右边界的时间。
    int binarySearch(vector<int> &nums, int left, int right, int target) {
        int mid = (left + right) / 2;
        if(left > right)
            return -1;
        if(nums[mid] == target)
            return mid;
        if(nums[mid] > target)
            return binarySearch(nums, left, mid - 1, target);
        if(nums[mid] < target)
            return binarySearch(nums, mid + 1, right, target);
    }
    // 设置isRight bool 值来判断是左边界还是有边界。
    int binarySearchLR(vector<int> &nums, int left, int right, int target, bool isRight) {
        int mid = (left + right) / 2;
        if(isRight) {
            if(left == right)
                return left;
            if(nums[mid] == target && nums[mid + 1] != target)
                return mid;
            else if(nums[mid] == target && nums[mid + 1] == target )
                return binarySearchLR(nums, mid + 1, right, target, true);
            else
                return binarySearchLR(nums, left, mid - 1, target, true);
        }
        else {
         // 这里需要考虑mid - 1是否会越界,因为只有两个元素时,mid总等于left,而如果left是0,则mid - 1会越界
            if(left == right || left == right - 1) {
                if(nums[left] == target)
                    return left;
                return right;
            }
            if(nums[mid] == target && nums[mid - 1] != target)
                return mid;
            else if(nums[mid] == target && nums[mid - 1] == target )
                return binarySearchLR(nums, left, mid - 1, target, false);
            else
                return binarySearchLR(nums, mid + 1, right, target, false);
        }
    }
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> result{-1, -1};
        if(nums.empty())
            return result;
        int n = nums.size();
        int findIndex = binarySearch(nums, 0, n - 1, target);
        if(findIndex == -1)
            return result;
        result[0] = binarySearchLR(nums, 0, findIndex, target, false);
        result[1] = binarySearchLR(nums, findIndex, n - 1, target, true);
        return result;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode(34). Search for a Range

problem Given an array of integers sorted in ascending order, find the starting and ending posi...

LeetCode Search for a Range

题目: Given a sorted array of integers, find the starting and ending position of a given target v...

LeetCode[Array]: Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. Yo...

[LeetCode][Java] Search for a Range

题目: Given a sorted array of integers, find the starting and ending position of a given target v...

LeetCode OJ-34.Search for a Range

LeetCode OJ-34.Search for a Range题目描述 Given an array of integers sorted in ascending order, find t...

Leetcode || Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value.Your...

leetcode 34. Search for a Range

import java.util.Stack; //Given a sorted array of integers, find the starting and ending position o...

LeetCode Search for a Range搜索特定数值的范围 三种方法求解

在排序数组中搜索一个值有多少个,并返回其两边下标,没有找到就返回[-1,-1]。注意时间效率是O(logN)。这就肯定要用到二分法的思想了。 主要难度是处理好下标的走势。 有三种方法可以求解: ...

LeetCode--Search for a range

DisciptionGiven an array of integers sorted in ascending order, find the starting and ending positio...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)