zoukankan      html  css  js  c++  java
  • LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, 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].


    题目标签:Array

      这道题目给了我们一个 有序序列,其中是有重复的,让我们找到target的范围,如果没有target,就返回 [-1,-1]。既然规定我们用O(log n),那么肯定是利用binary search。问题是,用binary search 是可以找到target, 但是这里需要找到一个范围,中间可能有很多个重复的target。我们可以用两次binary search, 第一次就找到第一个target, 第二次找到最后一个target,这样就可以返回target的范围了。

      怎么来找到第一个target呢,分析一下,如果中间的数字 大于 target, 那么说明target 还在更左边, 我们要把 right = mid - 1。如果中间数字 小于 target, 说明target 在更右边,要把 left = mid + 1。 到这里为止,都是普通的二分法,那么如果中间的数字是等于 target 的呢,因为我们要找到第一个target的位置,所以,就算找到了target, 我们也要向左边移动,因此,要把这个等于的情况加入 大于里面去。因为它们两种情况都是向左边移动。每次找到target 的时候,还需要 记住它的位置,之后一直更新。因为你找到的target 不一定是最左边的,所以要一直更新,最后一次就是最左边的target位置。

      找最后一个target也是同理。

    Java Solution:

    Runtime beats 79.19% 

    完成日期:07/14/2017

    关键词:Array

    关键点:用两次二分法,第一次来找最左边的target,第二次来找最右边的target

     1 public class Solution 
     2 {
     3     public int[] searchRange(int[] nums, int target) 
     4     {
     5         int[] res = {-1, -1};
     6         
     7         // first binary search to find the first target
     8         int left = 0;
     9         int right = nums.length - 1;
    10         
    11         while(left <= right)
    12         {
    13             int mid = left + (right - left) / 2;
    14             if(nums[mid] >= target) // if mid one is greater or equal to target
    15                 right = mid - 1;    // move to left half 
    16             else                    // if mid one is less than target
    17                 left = mid + 1;        // move to right half
    18             
    19             if(nums[mid] == target) // update index
    20                 res[0] = mid;
    21         }
    22         
    23         // second binary search to find the last target
    24         right = nums.length - 1;
    25         
    26         while(left <= right)
    27         {
    28             int mid = left + (right - left) / 2;
    29             if(nums[mid] <= target)
    30                 left = mid + 1;
    31             else
    32                 right = mid - 1;
    33                 
    34             if(nums[mid] == target)
    35                 res[1] = mid;
    36         }
    37         
    38         return res;
    39     }
    40 }

    参考资料:

    https://leetcode.com/problems/search-for-a-range/#/discuss

    LeetCode 算法题目列表 - LeetCode Algorithms Questions List

  • 相关阅读:
    centos下两种方法安装git
    Field.setAccessible()方法
    Tomcat日志格式自定义
    Java监控工具
    JDK目录结构和文件作用介绍
    Java中堆和栈的区别
    Magicodes.WeiChat——使用AntiXssAttribute阻止XSS(跨站脚本攻击)攻击
    Magicodes.WeiChat——缓存管理
    Magicodes.WeiChat——WeChatOAuthTest(网页授权获取用户基本信息)
    Magicodes.WeiChat——后台JS框架封装
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7178504.html
Copyright © 2011-2022 走看看