算法的复杂度就降低为 O(n) ,速度大大提高。
需求:前后数据x条不动,中间随机删除N条记录
实现:
Dictionary<string, int> dictA = GetArrayListFromTxt(this.txtFileNameA.Text);
Dictionary<string, int> dictB = GetArrayListFromTxt(this.txtFileNameB.Text);
Dictionary<string, int> dictC= new Dictionary<string,int>();
StringBuilder sb=new StringBuilder();
foreach (var item in dictB)
{
if (dictA.ContainsKey(item.Key))
{
dictA.Remove(item.Key);
dictC.Add(item.Key, item.Value);
sb.AppendLine(item.Key);
}
}
textBox1.Text = sb.ToString();
txtRepeatNum.Text = Convert.ToString(dictC.Count );
foreach (object key in RandomValues(dictA,x).Take(n))
{
dictA.Remove(key.ToString());
}
算法:
publicstaticintnewwhileyieldreturn
}
本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2011/10/30/2229585.html,如需转载请自行联系原作者