如下:int Key[1000]; //取值为0-99,不能排序int ints[5]; //取值为0-99,能排序,此数组中的值没有重复值要求:
例: Key = {0,55,77,66,54,31,89,65,77,21,34,35,66,54,99,31,....... };
inits = { 66,54,31,99,98}; 1. Key中第一次出现ints中值的位置;
结果:3 2. Key中连续出现ints中值的位置;
结果: 3-5 和 12-15
3. Key中间隔的位置
结果: 6-11
那种方法最快?
例: Key = {0,55,77,66,54,31,89,65,77,21,34,35,66,54,99,31,....... };
inits = { 66,54,31,99,98}; 1. Key中第一次出现ints中值的位置;
结果:3 2. Key中连续出现ints中值的位置;
结果: 3-5 和 12-15
3. Key中间隔的位置
结果: 6-11
那种方法最快?
或Enumerable.Range(1, 99).OrderBy(Guid.NewGuid());
int[] a = { 1, 2, 3, 4, 5, 9 };
int[] b = { 1, 4, 5, 7, 8, 9 };
var c = a.Cast<int>().Concat(b.Cast<int>()).GroupBy((t) => t).Where((t) => t.Count() == 1);
数组重复数据
获取序号http://topic.csdn.net/u/20101202/16/3673b3c2-d65c-4bf5-b3f0-40531462d08f.html
1. Key中第一次出现ints中值的位置;
结果:3
//像这。为啥结果是3呢?
foreach(int each in key){
//在ints中快速查找 each(这个不必说了吧)
如果找到,针对你的问题(2)进行处理,这个算法也不用说了吧。
//问题3的情况与此类似。
}这只是个思路,你需要再思考下才会明白。
2. 看一下Array类提供了一些方法,估计效率应该比自己写code要高
Array Members
BinarySearch Overloaded. Searches a one-dimensional sorted Array for a value, using a binary search algorithm.
Exists Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
Find Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.
FindAll Retrieves all the elements that match the conditions defined by the specified predicate.
FindIndex Overloaded.
FindLast Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array.
FindLastIndex Overloaded.
ForEach Performs the specified action on each element of the specified array.