小算法

问题描述:

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

 1 public static void main(String[] args)
 2     {
 3         //B数组:[5,2,4,1]
 4         
 5         Random rand = new Random();
 6         int maxValue = 120000;//假定A和B 的最大值
 7         int length = 70000;//a,b 的长度
 8         int [] a = new int[length];
 9         int [] b = new int[length];
10         boolean[] c= new boolean[length];
11         boolean[] temp = new boolean[maxValue];
12         //随机初始化啊a,b数组
13         for(int i = 0;i<length;i++){
14             a[i] = rand.nextInt(maxValue);
15             b[i] = rand.nextInt(maxValue);
16         }
17         long t1 = System.currentTimeMillis();
18         //循环B,验证元素是否存在(循环B,将B中值作为a的下标,对应位置标记为true)
19         for (int item : b) {
20             temp[item] = true;
21         }
22         
23         for(int i = 0;i<a.length;i++){
24             if(temp[a[i]])
25                 c[i] = true;
26         }
27         long t2 = System.currentTimeMillis();
28         System.out.println(t2-t1);
29     }

不算初始化,5毫秒左右

---

参考:

一个从四秒到10毫秒,花了1年的算法问题?

原文地址:https://www.cnblogs.com/xingele0917/p/4996805.html