简体   繁体   English

用于对数组C ++中的相同数字进行计数的函数

[英]Function to count same numbers in array C++

I have this function that is supposed to count how many duplicates of same number occur in certain array. 我有这个功能,应该计算在某个数组中出现多少个相同编号的重复项。 Important this must be of complexity O(logn). 重要的是,它必须具有复杂度O(logn)。 I wrote this one below, but it doesn't count the duplicates properly. 我在下面写下了这个,但是它没有正确地计算出重复项。 Also one more thing, the numbers are sorted from lowest to highest. 还有一件事,数字从最低到最高排序。

int CountElementsinFile(int *Arr, int num, int numOfD)
{
    int avg{}; 
    int inB = 0; 
    int inE = numOfD - 1; 
    int el{};
    while (inB <= inE)
    {
        avg = (inB + inE) / 2;

        if (Arr[avg] == num)
            el++;
            if (Arr[avg] > num)
                inE = avg - 1;
            else
                inB = avg + 1;
    }

    return el;
}

With std, you might do: 使用std,您可以执行以下操作:

int CountElementsinFile(const int *a, int size, int value)
{
    const auto range = std::equal_range(a, a + size, value);
    return range.second - range.first;
}

You need to determine the upper and lower boundaries of num subsequence using the Bisection method . 您需要确定的上限和下限num使用序列二分法 You need to rearrange the lower or upper (depending on the comparison) search region boundary in the while loop until inB < inE reducing the region in half. 您需要在while循环中重新排列搜索区域的上下边界(取决于比较),直到inB < inE将区域缩小一半。 The complexity will be O(ln(n)) . 复杂度将为O(ln(n)) You were close, but you will not be able to find both boundaries in one while loop. 您已经接近了,但是您将无法在一个while循环中找到两个边界。 I just corrected your code. 我刚刚更正了您的代码。

int CountElementsinFile(int *Arr, int num, int numOfD)
{
   // There is no num in the array
   if (Arr[0] > num || Arr[numOfD - 1] < num)
      return 0;

   int avg{};
   int lb, ub;

   // Find lower boundary
   int inB = 0;
   int inE = numOfD - 1;
   while (inB < inE)
   {
      // divide search region
      avg = (inB + inE) / 2;
      if (Arr[avg] >= num)
         inE = avg;
      else
         inB = avg+1;
   }

   lb = inE;

   // Find upper boundary   
   // inB already found
   inE = numOfD - 1;
   while (inB < inE)
   {
      avg = (inB + inE + 1) / 2;
      if (Arr[avg] > num)
         inE = avg-1;
      else
         inB = avg;
   }

   ub = inB;

   return ub - lb + 1;
}

int main()
{
   int arr[] = { 5, 7, 8, 9, 9, 9, 9, 9, 11 };
   std::cout << CountElementsinFile(arr, 9, sizeof(arr) / sizeof(int)) << std::endl;
   return 0;
}

From the function signature you gave, I am guessing you were given a number N and a sorted array of numbers and need to count how many times N appears in the array. 根据您给出的函数签名,我猜您将得到一个数字N和一个排序的数字数组,需要计算N在数组中出现的次数。

Since the array is sorted, you need to find the index (using binary search) of the first number that is smaller than N (this is O(log n) ), the index of the first number larger than N (this is also O(log n) ), and simply substract one index from the other. 由于数组已排序,因此需要找到小于N的第一个数字的索引(使用二进制搜索)(这是O(log n) ),大于N的第一个数字的索引(这也是O(log n) ),然后简单地从另一个索引中减去一个索引。 Of course you need to take into account edge cases where there are no numbers smaller than N or larger than N, but that is for you to figure out. 当然,您需要考虑不存在小于N 大于N的数字的极端情况,但这是您要找出的。

#include<algorithm>
using namespace std;

int CountElementsinFile(int arr[], int size, int numToSearch)
{
    return count(arr, arr + size, numToSearch); 
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM