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

  • 相关阅读:
    微服务- 认识我们的服务2
    微服务- 用于定位日志的会话的传播简单实现4
    微服务-Feign性能调优3
    微服务-为什么要微服务1
    GateWay与熔断器
    slf4j-api、slf4j-log4j12、log4j之间关系
    关于tomcat的axConnections、maxThreads、acceptCount
    Spring注解@Resource和@Autowired区别对比详解
    关于spring MVC 关键组件 & 流程
    关于spring MVC的全局异常处理
  • 原文地址:https://www.cnblogs.com/lz87/p/7478982.html
Copyright © 2011-2022 走看看