求一高效算法取交集。string[] a={ '1','2','4','3'}如果:
string[] b={ '2','3'}
a Intersect b= null如果:
string[] b={ '2','4'}
a Intersect b= { '2','4'}如果:
string[] b={ '1'}
a Intersect b= {'1'}要求顺序取交集,如果顺序不匹配,交集为空。
string[] b={ '2','3'}
a Intersect b= null如果:
string[] b={ '2','4'}
a Intersect b= { '2','4'}如果:
string[] b={ '1'}
a Intersect b= {'1'}要求顺序取交集,如果顺序不匹配,交集为空。
char[] or string[]?
补充一下:
如果:
string[] b={'4', '3','5'}
a Intersect b= {'4','3'}如果:
string[] b={ '2','3'}
a Intersect b= null如果:
string[] b={ '2','4'}
a Intersect b= { '2','4'}如果:
string[] b={ '1'}
a Intersect b= {'1'}要求顺序取交集,如果顺序不匹配,交集为空。
{
List<string> list = new List<string>();
foreach (var v in a)
{
if (b.Contains(v))
{
list.Add(v);
}
}
return list.ToArray();
}如果元素过多,并且,以上代码时间过长,
需要将其中一个string[] 转换为Dictionary<string, int>提高查找效率
a Intersect b= {"4","3"}
如果:
string[] b={ "2","3"}
a Intersect b= null如果:
string[] b={ "2","4"}
a Intersect b= { "2","4"}如果:
string[] b={ "1"}
a Intersect b= {"1"}要求顺序取交集,如果顺序不匹配,交集为空。
8楼说的不错,应该叫投射比较恰当。
7楼明显没有理解。
那么就有LCS,这个可以帮助你
如果你不想要顺序的,那么将一个字符串hash,之后再另一个里面找存在否,复杂度更低,速度更快!
string[] a={ "1","2","4","3"};
如果:
string[] b={ "4","3","5","1","2"};
结果是什么??