LeetCode 34 Search for a Range(搜索范围)

简介:

翻译

给定一个整型已排序数组,找到一个给定值在其中的起点与终点。

你的算法复杂度必须低于O(logn)。

如果目标在数组中不会被发现,返回[-1, -1]。

例如,给定[5, 7, 7, 8, 8, 10],目标值为8,返回[3, 4]

原文

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

代码

class Solution {
public:
    vector<int> searchRange(vector<int> &nums, int target) {
        vector<int> res;
        int l = 0, r = nums.size() - 1;
        if(nums.size() <= 0) {
            res.push_back(-1);
            res.push_back(-1);
            return res;     
        } else if(nums.size() == 1) {
            if(nums[0] == target) {
                res.push_back(0);
                res.push_back(0);
                return res;
            } else {
                res.push_back(-1);
                res.push_back(-1);
                return res;
            }
        }
        while(l <= r) {
            if(nums[l] < target) {
                ++l;
            } 
            if(nums[r] > target) {
                --r;
            }
            if(nums[l] == target && nums[r] == target) {
                res.push_back(l);
                res.push_back(r);
                return res;
            }
        }
        res.push_back(-1);
        res.push_back(-1);
        return res;
    }
};

随手写的,虽然能通过,不过还是看看大神的解法来提高自己。

class Solution {
private:
    int binarySearchLow(vector<int>& nums, int target, int begin, int end)
    {
        if(begin > end) return begin;
        int mid = begin + (end - begin) / 2;
        if(nums[mid] < target) return binarySearchLow(nums, target, mid + 1, end);
        else return binarySearchLow(nums, target, begin, mid - 1);
    }
    int binarySearchUp(vector<int>& nums, int target, int begin, int end)
    {
        if(begin > end) return end;
        int mid = begin + (end - begin) / 2;
        if(nums[mid] > target) return binarySearchUp(nums, target, begin, mid - 1);
        else return binarySearchUp(nums, target, mid + 1, end);
    }
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res(2, -1);
        if(nums.empty()) return res;
        int high = binarySearchUp(nums, target, 0, nums.size() -1);
        int low = binarySearchLow(nums, target, 0, nums.size() - 1);
        if(high >= low)
        {
            res[0] = low;
            res[1] = high;
            return res;
        }
        return res;
    }
};
目录
相关文章
|
1月前
|
算法
力扣240 搜索二维矩阵II
力扣240 搜索二维矩阵II
|
3月前
|
Go
golang力扣leetcode 240.搜索二维矩阵II
golang力扣leetcode 240.搜索二维矩阵II
19 0
|
3月前
|
Go
golang力扣leetcode 79.单词搜索
golang力扣leetcode 79.单词搜索
25 0
|
3月前
|
算法
【Leetcode 74】搜索二维矩阵 —— 二分查找|矩阵
给你一个满足下述两条属性的`m x n`整数矩阵:每行中的整数从左到右按非严格递增顺序排列。每行的第一个整数大于前一行的最后一个整数
|
3月前
|
Go
golang力扣leetcode 81.搜索旋转排序数组II
golang力扣leetcode 81.搜索旋转排序数组II
17 0
|
3月前
|
Go
golang力扣leetcode 33.搜索旋转排序数组
golang力扣leetcode 33.搜索旋转排序数组
14 0
|
3月前
|
Go
golang力扣leetcode 74.搜索二维矩阵
golang力扣leetcode 74.搜索二维矩阵
18 0
|
27天前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
1月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3

热门文章

最新文章