[英]Finding Duplicates in a array - Syntax Explanation
I don't have any idea about this syntax int *count = new int[sizeof(int)* (size - 2)]
我对这种语法一无所知
int *count = new int[sizeof(int)* (size - 2)]
What kind of array that will create.将创建什么样的数组。
I thought they are trying to create map like structure.我以为他们正在尝试创建类似 map 的结构。 But how does it work?
但它是如何工作的?
#include <bits/stdc++.h>
using namespace std;
void printRepeating(int arr[], int size)
{
int *count = new int[sizeof(int)*(size - 2)];
int i;
cout << " Repeating elements are ";
for(i = 0; i < size; i++)
{
if(count[arr[i]] == 1)
cout << arr[i] << " ";
else
count[arr[i]]++;
}
}
// Driver code
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
return 0;
}
// This is code is contributed by rathbhupendra
Finding duplications in an array is of course a solved problem.在数组中查找重复项当然是一个已解决的问题。 IMHO a very simple solution is:
恕我直言,一个非常简单的解决方案是:
for(int i = 1; i < num_elements; i++){ if(arr[i-1]==arr[i]){...duplicate....}}
This requires O(n) memory and O(n*log(n)) time, so it's quite ok.这需要 O(n) memory 和 O(n*log(n)) 时间,所以还可以。 You can also use a hashmap, but that's pretty much the same.
您也可以使用 hashmap,但这几乎相同。
Anyways, to your question(s):无论如何,对于您的问题:
int *count = new int[sizeof(int)* (size - 2)];
This is incorrect.这是不正确的。 I assume it used to be this C code:
我假设它曾经是这个 C 代码:
int num_elements = size-2; // we want size-2 elements (not sure why)
int total_bytes = sizeof(int) * num_elements;
int *count = calloc(total_bytes); // reserve space, and set to 0
Which one could translate to this C++ code:哪一个可以翻译成这个 C++ 代码:
int num_elements = size-2;
int * count = new int[num_elements]{0}; // alloc and set to zero
So the person who did the port misunderstands fundamentals about C++.所以做移植的人误解了 C++ 的基本原理。
Let's dig further.让我们进一步挖掘。
For the sake of it, the problem formulation appears to be:为此,问题表述似乎是:
You are given an array of n+2 elements.
您将获得一个包含 n+2 个元素的数组。 All elements of the array are in range 1 to n.
数组的所有元素都在 1 到 n 的范围内。 And all elements occur once except two numbers which occur twice.
并且所有元素都出现一次,除了两个出现两次的数字。 Find the two repeating numbers
找到两个重复的数字
I have made tiny changes to make the solution less crazy, and I've added annotations.我进行了微小的更改以使解决方案不那么疯狂,并且添加了注释。
// #include <bits/stdc++.h> is a bad choice.
// This includes _EVERYTHING_ in C++,
// but it only works in GCC afaik. For this particular
// case we just need cout, so:
#include <iostream>
// using the std namespace like this spills function calls like crazy in the global namespace.
// it is both, conventient and "not too bad" (imho) in cpp files,
// but never ever do this in .h files where it affects multiple cpp files.
using namespace std;
void printRepeating(int arr[], int size)
{
// create a new array of size-2 and set to zero
int *count = new int[(size - 2)]{0};
int i;
cout << " Repeating elements are ";
// loop all elements
for(i = 0; i < size; i++)
{
int value = arr[i];
// increase the count for `value`
// note that if value<=0 or value>size-2,
// then the program will crash!
// the problem description is contrived, but it does
// state that all values need to be >0 and <=size-2,
// so this next array access is fine!
count[value-1]++;
// we still have to subtract 1, because C arrays start
// at index 0, but our problem description says we start at 1.
// we could also create an array of size-1 and
// "ignore" the first position in count[0] (it would never get used!)
// if the element appears the second time...
if(count[value-1] == 2){
// then print it
cout << value << " ";
// btw: if you check with count[value-1]==2, then only
// the first duplicate is printed.
// you could compare with count[value-1]>=2 then all
// repeating elements are printed repeatetly.
}
}
// free up the memory. we allocated with `new[]`
// so we also have to use `delete[]`
delete [] count;
}
// Driver code
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
// next line is a standard trick.
// a single int consumes 4bytes (typically),
// the array will have size 7*4=28 bytes, so sizeof(arr)=28
// the first element is an int, so sizeof(arr[0]) = 4
// so sizeof(arr)/sizeof(arr[0]) = 7
// c and c++ don't have arr.length like java,etc., that's
// why under certain circumstances this "trick" is used.
int arr_size = sizeof(arr)/sizeof(arr[0]);
// call the function
printRepeating(arr, arr_size);
return 0;
}
// This is code is contributed by rathbhupendra.
// Maybe fixed and annotated by hansi:)
I hope this helps you and answers some questions.我希望这对您有所帮助并回答一些问题。 Good luck with your C++ adventures!
祝您的 C++ 冒险好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.