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         }
  • 相关阅读:
    缺失的第一个正数
    tuple用法
    整数转罗马数字
    三种时间格式的转换
    不同包的调用
    正则表达式
    lgb模板
    线性回归
    时间序列的特征
    3D聚类
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4828305.html
Copyright © 2011-2022 走看看