检查两个未排序的整数数组是否具有相同元素的算法?

问题描述:

在C语言编程方面,我仍然是一个新手,因此请彻底解释.另外,这是一项家庭作业,所以如果您能帮助我解决这个问题,我将非常乐意.我到处逛逛,只能找到与此问题类似的帖子.我找到的那些对象提到了哈希表之类的东西,但我没有学到,所以我怀疑这些类型的东西是否可以使用.

I'm still a newbie when it comes to programming in C so please be thorough in explanations. Also, this is a homework assignment so I would love it if you could help me solve this problem. I've looked around the site and could only find posts similar to this question. The ones that I did find mentioned things like hash tables, which I did not learn so I doubt those kinds of things can be used.

对于我的作业,我有两个相同长度的未排序整数数组.它们只能具有0-99范围内的整数.允许重复的元素.目的是查看它们是否具有相同的元素,从而检查它们是否相等,而不进行排序.我的教授还说,它必须在线性时间内运行,因此无法对它们进行排序.

For my assignment, I have two unsorted integer arrays of the same length. They can only have integers in the range of 0-99. Duplicate elements are allowed. The goal is to see whether they have the same elements, therefore checking if they are equal, without sorting them. My professor also said that it has to run in linear time, so sorting them would not be an option.

例如

A[4]={1,2,3,4}
B[4]={4,3,2,1}

这些数组将相等.但是,

These arrays would be equal. However,

A[3]={1,1,2}
B[3]={2,2,1}

这些数组不相等.

我想出的算法是这样的:
第一个for循环遍历第一个数组,而内部的for循环遍历第二个数组.它会检查第一个数组中的当前元素是否等于第二个数组中的元素.如果它们相等,则外部循环迭代,内部for循环再次从第一个元素开始,检查第一个数组中的当前元素是否与第二个元素匹配.如果不是,则退出两个循环并返回0,表示它们不相等.该算法不是线性运行的.同样,它也不检查重复项,因为内部的for循环每次都会检查第二个数组中的所有元素.

The algorithm that I came up with goes like this:
The first for loop iterates through the first array and the inner for loop iterates through the second array. It would check to see if the current element in the first array is equal to an element in the second. If they are equal, then the outer loop iterates and the inner for loop starts at the first element again, checking to see if the current element in the first array ever matches with an element in the second. If not, then it exits out of both loops and returns 0 indicating that they are not equal. This algorithm does not run in linear time. Also, it does not check for duplicates since the inner for loop checks all the elements in the second array each time.

我正在考虑每次发现第二个数组中的元素等于第一个数组中的元素时,将其值更改为非常大的值.我认为这样可以消除重复的问题.但是我仍然需要帮助制定更快的算法并理解我的错误.谢谢.

I was thinking of changing the value of the element in the second array to something extremely large every time it was found to be equal to an element in the first array. I think that way it would get rid of the duplicate problem. But I still need help making a faster algorithm and understanding my error. Thanks.

简单地重新考虑您的任务:您必须找出一个数组是否是另一个数组的置换.为了解决这个问题,您只需要检查每个数组中有多少个零,一,...,99.也就是说,

Simply rethink your task: you have to find out if one array is a permutation of another. To solve this you have only to check how many zeros, ones, ..., 99s in each array. That is,

  • 让新数组int count[100]初始化为全零
  • 对于每个i:递增count[a[i]]并递减count[b[i]]
  • 如果count[]由全零组成,则a []和b []是置换.
  • let new array int count[100] be initialized to all zeros
  • for each i: increment count[a[i]] and decrement count[b[i]]
  • if count[] consists of all zeroes then a[] and b[] are permutations.