经典问题和算法

static void ValidateArrayElement()
 {
     Stopwatch sp = new Stopwatch();
     sp.Start();
     Random rand = new Random();
     Int32 maxValue = 120000;//元素最大值,是一个假定值
     Int32 length = 70000;// A,B的长度
     Int32[] A = new Int32[length];
     Int32[] B = new Int32[length];
     Boolean[] C = new Boolean[length];
    Boolean[] Atemp = new Boolean[maxValue];//临时的辅助变量
     //随机初始化A,B数组
     for (int i = 0; i < length; i++)
    {
         A[i] = rand.Next(maxValue);
         B[i] = rand.Next(maxValue);
     }          
    //循环B,验证元素是否存在
     foreach (var item in B) Atemp[item] = true;
     //循环A,验证是否存在,将C对应位置标记为true
   for (int i = 0; i < A.Length; i++) if (Atemp[A[i]]) C[i] = true;
    sp.Stop();//停止计时
     Console.WriteLine(sp.ElapsedMilliseconds);
 }

有两组随机生成的(0~99999)Int32数据A和B,将A按顺序判断在B中是否存在并记录在Boolean型的C中

点评,算法很新颖,用空间换取时间

原文地址:https://www.cnblogs.com/weiweiboqi/p/4521747.html