zoukankan      html  css  js  c++  java
  • [LintCode] Search for a Range

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

    If the target is not found in the array, return [-1, -1].

    Example

    Given [5, 7, 7, 8, 8, 10] and target value 8,
    return [3, 4].

    Challenge 

    O(log n) time.

     Break this problem into two subproblems:

    1. find the first element in the array of the given target value;

    2. find the last element in the array of the given target value;

    Both of these two subproblems can be solved by twisting the regular binary search algorithm when the target value is found.

    To find the first occurence,  keep looking its left range, including the mid element;

    To find the last occurence,  keep looking its right range, including the mid element;

     1 public class Solution {
     2     /** 
     3      *@param A : an integer sorted array
     4      *@param target :  an integer to be inserted
     5      *return : a list of length 2, [index1, index2]
     6      */
     7     public int[] searchRange(int[] A, int target) {
     8         int[] result = new int[2];
     9         result[0] = -1;
    10         result[1] = -1;
    11         if(A == null || A.length == 0)
    12         {
    13             return result;
    14         }
    15         result[0] = searchRangeLeft(A, target, 0, A.length - 1);
    16         if(result[0] < 0)
    17         {
    18             return result;
    19         }
    20         else
    21         {
    22             result[1] = searchRangeRight(A, target, 0, A.length - 1);
    23         }
    24         return result;
    25     }
    26     private int searchRangeLeft(int[] A, int target, int lo, int hi)
    27     {
    28         if(hi - lo <= 1)
    29         {
    30             if(A[lo] == target)
    31             {
    32                 return lo;
    33             }
    34             else if(A[hi] == target)
    35             {
    36                 return hi;
    37             }
    38             else
    39             {
    40                 return -1;
    41             }
    42         }
    43         int mid = lo + (hi - lo) / 2;
    44         if(A[mid] == target)
    45         {
    46             return searchRangeLeft(A, target, lo, mid);
    47         }
    48         else if(A[mid] > target)
    49         {
    50             return searchRangeLeft(A, target, lo, mid - 1);
    51         }
    52         else
    53         {
    54             return searchRangeLeft(A, target, mid + 1, hi);
    55         }
    56     }
    57     private int searchRangeRight(int[] A, int target, int lo, int hi)
    58     {
    59         if(hi - lo <= 1)
    60         {
    61             if(A[hi] == target)
    62             {
    63                 return hi;
    64             }
    65             else if(A[lo] == target)
    66             {
    67                 return lo;
    68             }
    69             else
    70             {
    71                 return -1;
    72             }
    73         }
    74         int mid = lo + (hi - lo) / 2;
    75         if(A[mid] == target)
    76         {
    77             return searchRangeRight(A, target, mid, hi);
    78         }
    79         else if(A[mid] > target)
    80         {
    81             return searchRangeRight(A, target, lo, mid - 1);
    82         }
    83         else
    84         {
    85             return searchRangeRight(A, target, mid + 1, hi);
    86         }
    87     }
    88 }

    Related Problems

    Total Occurrence of Target

    Range Sum Query 2D - Immutable

  • 相关阅读:
    2019-9-2-简单搭建自己的博客
    2018-7-15-WPF-在-DrawingContext-的-push-如何使用
    2018-7-15-WPF-在-DrawingContext-的-push-如何使用
    2019-7-3-Roslyn-理解-msbuild-的清理过程
    2019-7-3-Roslyn-理解-msbuild-的清理过程
    MySQL数据库事务详解
    求一个Map中最大的value值,同时列出键,值
    Struts1入门实例(简单登录)
    java字符流操作flush()方法及其注意事项
    HDU 1874 畅通工程续 2008浙大研究生复试热身赛(2)
  • 原文地址:https://www.cnblogs.com/lz87/p/7478982.html
Copyright © 2011-2022 走看看