VB.NET数组路口 [英] VB.NET Array Intersection
本文介绍了VB.NET数组路口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是非常微不足道的,但我无法找到,在小于n ^ 2时执行一个答案。比方说,我有两个字符串数组,我想知道哪些字符串数组都存在。我会怎么做,有效,在VB.NET或是的有没有办法比双环做到这一点其他的?
This could be terribly trivial, but I'm having trouble finding an answer that executes in less than n^2 time. Let's say I have two string arrays and I want to know which strings exist in both arrays. How would I do that, efficiently, in VB.NET or is there a way to do this other than a double loop?
推荐答案
如果你两个数组进行排序,然后你可以穿行其中每一次找到所有匹配的字符串。
If you sort both arrays, you can then walk through them each once to find all the matching strings.
伪code:
while(index1 < list1.Length && index2 < list2.Length)
{
if(list1[index1] == list2[index2])
{
// You've found a match
index1++;
index2++;
} else if(list1[index1] < list2[index2]) {
index1++;
} else {
index2++;
}
}
然后你就降低到它需要做排序的时间。
Then you've reduced it to the time it takes to do the sorting.
这篇关于VB.NET数组路口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文