A B两个相同长度整数数组,判断相同下标对应值是否相等,true或者false 存在布尔数组C中
2015-10-19 10:25
991 查看
问题描述:A B两个相同长度整数数组,判断相同下标对应值是否相等,true或者false 存在布尔数组C中。
一:常规操作就是利用数组的方法 if (B.Contains(A[i])) C[i] = true;
二:利用hashset hashtable directionary list等集合表,速度提升明显,
三:博客园一哥们为解决另一个哥们问题想到的方法。因为数组A、B都是int,所以可以把A、B作为C的索引考虑。不晓得这哥们怎么想到的,不过亲测速度的确快。
一:常规操作就是利用数组的方法 if (B.Contains(A[i])) C[i] = true;
二:利用hashset hashtable directionary list等集合表,速度提升明显,
三:博客园一哥们为解决另一个哥们问题想到的方法。因为数组A、B都是int,所以可以把A、B作为C的索引考虑。不晓得这哥们怎么想到的,不过亲测速度的确快。
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); Console.ReadKey(); }
相关文章推荐
- UI15_XML, JSON解析
- String、StringBuffer和StringBuilder的区别
- 关于本科毕业论文《Laguerre小波在数值积分与微分方程数值解中的应用》存在的问题与小结
- Unique Binary Search Trees II
- easyui-combo个人实例
- WWDC2015 - Interface Builder技巧(IB 技巧)
- 长时间停留在calculating requirements and dependencies
- Extjs form.submit()提交与Ext.Ajax.request的区别
- 仿QQUI实现(1)
- Single value range only allowed in SystemVerilog
- stack和queue
- UIScrollView
- Easyui Datagrid使用小例
- Git GUI的使用
- iOS UITableView-FDTemplateLayoutCell框架 cell重叠 高度返回0.5问题解决
- Android开发系列(三) 跟随移动userguide
- Unique Binary Search Trees
- IOS基础UI之(八)UIScrollView
- 浅谈单线程模型中Message、Handler、Message Queue、Looper之前的关系
- 测来测去,感觉REQUESTS最实在