Leetcode 34 Search for a Range (二分搜索 lower_bound和upper_bound)

244 篇文章 0 订阅
77 篇文章 8 订阅

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


题目链接:https://leetcode.com/problems/search-for-a-range/

题目大意:找到数字value在排序好的nums中的最左最右位置

题目分析:直接对C++ STL中的lower_bound和upper_bound函数进行修改

public class Solution {
    
    //找nums中第一个等于target的数字
    int myLowerBound(int[] nums, int sz, int target) {   
        int ans = -1;
        int fir = 0, mid = 0, half = 0, len = sz;
        while(len > 0) {
            half = len >> 1;
            mid = fir + half;
            if(nums[mid] < target) {
                fir = mid + 1;
                //mid处的也要减去
                len = len - half - 1;
            }
            else {
                if(nums[mid] == target) {
                    ans = mid;
                }
                len = half;
            }
        }
        return ans;
    }
    
    //找nums中最后一个等于target的数字
    int myUpperBound(int[] nums, int sz, int target) {
        int ans = -1;
        int fir = 0, mid = 0, half = 0, len = sz;
        while(len > 0) {
            half = len >> 1;
            mid = fir + half;
            if(nums[mid] <= target) {
                fir = mid + 1;
                //mid处的也要减去
                len = len - half - 1;
            }
            else {
                if(mid != 0 && nums[mid - 1] == target) {
                    ans  = mid - 1;
                }
                len = half;
            }
        }
        if(mid == sz - 1 && nums[mid] == target) {
            ans = sz - 1;
        }
        return ans;
    }
    
    public int[] searchRange(int[] nums, int target) {
        int len = nums.length;
        int[] ans = {-1, -1};
        if(nums[len - 1] < target || nums[0] > target) {
            return ans;
        }
        ans[0] = myLowerBound(nums, len, target);
        ans[1] = myUpperBound(nums, len, target);
        return ans;
    }
}


C++中的lower_bound:

template <class ForwardIterator, class T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::difference_type count, step;
  count = distance(first,last);
  while (count>0)
  {
    it = first; step=count/2; advance (it,step);
    if (*it<val) {                 // or: if (comp(*it,val)), for version (2)
      first=++it;
      count-=step+1;
    }
    else count=step;
  }
  return first;
}

upper_bound:

template <class ForwardIterator, class T>
  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::difference_type count, step;
  count = std::distance(first,last);
  while (count>0)
  {
    it = first; step=count/2; std::advance (it,step);
    if (!(val<*it))                 // or: if (!comp(val,*it)), for version (2)
      { first=++it; count-=step+1;  }
    else count=step;
  }
  return first;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值