zoukankan      html  css  js  c++  java
  • 【面试题38】数字在排序数组中出现的次数

    【题目描述】

    统计一个数字在排序数组中出现的次数。

    例如输入数组{1,2,3,3,3,3,4,5}和数字3,由于3在数组中出现了4次,因此输出4。

    【解决方案】

    解法一:先找到key的位置,然后往两边搜索,直到找到所有的key。时间复杂度O(n)。

    解法二:找到连续key左右两边的下边,然后相减加1,即为key的数量。时间复杂度O(logn)。

    我的代码,仅供参考:

     1         public static int GetNumberOfKey(int[] arr, int key)
     2         {
     3             if (arr == null || arr.Length < 1)
     4                 return 0;
     5 
     6             int result = 0;
     7 
     8             int leftIndex = GetLeftIndexOfKey(arr, key);
     9             int rightIndex = GetRightIndexOfKey(arr, key);
    10 
    11             if (leftIndex > -1 && rightIndex > -1)
    12                 result = rightIndex - leftIndex + 1;
    13 
    14             return result;
    15         }
    16 
    17         public static int GetLeftIndexOfKey(int[] arr, int key)
    18         {
    19             int start = 0;
    20             int end = arr.Length - 1;
    21             int mid = 0;
    22             int rsIndex = -1;
    23 
    24             while (start <= end)
    25             {
    26                 mid = (start + end) / 2;
    27                 if (arr[mid] > key)
    28                 {
    29                     end = mid - 1;
    30                 }
    31                 else if (arr[mid] < key)
    32                 {
    33                     start = mid + 1;
    34                 }
    35                 else if (mid > 0 && arr[mid - 1] == key)
    36                 {
    37                     end = mid - 1;
    38                 }
    39                 else
    40                 {
    41                     rsIndex = mid;
    42                     break;
    43                 }
    44             }
    45 
    46             return rsIndex;
    47         }
    48 
    49         public static int GetRightIndexOfKey(int[] arr, int key)
    50         {
    51             int start = 0;
    52             int end = arr.Length - 1;
    53             int mid = 0;
    54             int rsIndex = -1;
    55 
    56             while (start <= end)
    57             {
    58                 mid = (start + end) / 2;
    59                 if (arr[mid] > key)
    60                 {
    61                     end = mid - 1;
    62                 }
    63                 else if (arr[mid] < key)
    64                 {
    65                     start = mid + 1;
    66                 }
    67                 else if (mid < arr.Length - 1 && arr[mid + 1] == key)
    68                 {
    69                     start = mid + 1;
    70                 }
    71                 else
    72                 {
    73                     rsIndex = mid;
    74                     break;
    75                 }
    76             }
    77 
    78             return rsIndex;
    79         }
  • 相关阅读:
    整合Druid数据源
    SpringBoot与数据访问
    IDEA spirng boot @Autowired注解 mapper出现红色下划线解决方法
    IntelliJ Idea 常用快捷键列表
    docker 安装mysql示例
    设计模式都没用过,好意思出去面试?
    为什么 Java 线程没有 Running 状态?
    厉害了,淘宝千万并发,14 次架构演进…
    Redis 内存满了怎么办……
    Java 线程池 8 大拒绝策略,面试必问!
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4828305.html
Copyright © 2011-2022 走看看