编辑:我只是意识到,你说的这些组在3个值硬编码。这是任何大小的集合的超级通用算法。
对于3值集合,可以执行一组元素的相同的倾销和分选,然后执行:
if(setB.a < setA.a)
if(setB.b < setA.b)
if(setB.c < setA.c)
return true;
return false;
================ ======================================
的一般算法:
这是我可以立即想到的最有效的方法。
伪代码(比Java更Python,sorry--希望注释说明):
list l1 = set1.items() //get the items out
list l2 = set2.items()
l1 = sort(l1)
l2 = sort(l2) //sort the lists
int set2idx1 = l1[0].find_closest_greater_than_value(l2) //binary search or something
if set2idx1 exists:
l2 = l2[set2idx1+1:] //in python this means l2 is reassigned to a subarray of l2 starting at set2idx1+1 going to the end of l2
else:
return false
for(int i=1; i<l1.len; i++)
int set2idxi = l1[i].find_closest_greater_than_value(l2) //binary search or something
if set2idxi exists:
l2 = l2[set2idxi+1:]
else
return false
return true
评论,如果任何事情没有意义
编辑编辑:一般的
解释算法:
- 将设置的元素转储到数组中
- 对这些数组进行排序
- 遍历第一个数组,查看第二个数组中是否存在大于当前值的值。如果是这样,获取该值的索引并删除之前包含该索引的所有内容,并将第二个数组变量重新分配给剩下的内容。
- 如果没有这样的值(或者因为它不存在或者您已经用完了测试值,则返回false)。否则,最后,返回true。
这里的想法是,因为数组是排序的,所以任何大于第二个数组中匹配元素的元素都会大于第一个数组中的元素。所以你可以砍掉较低的值,因为你不想使用相同的值,所以你也可以砍掉你找到的那个值。如果您返回false,则知道这是因为没有更大的值,或者是因为array1中的数字全部大于array2中的数字,或者是因为array2中的数字不够大于array1中的数字。
你能张贴当前的方法? (以获得你想要做这件事更容易的感觉) – irrelephant
发布了一个示例。这是漫长而丑陋的,令人尴尬的。但你明白了。 – eagerbeaver