且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-09 19:00:12

简单地重新考虑您的任务:您必须找出一个数组是否是另一个数组的置换.为了解决这个问题,您只需要检查每个数组中有多少个零,一,...,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.