现在有1千万个随机数,随机数的范围在1到1亿之间。现在要求写出一种算法,将1到1亿之间没有在随机数中的数求出来
2016-03-10 16:05
856 查看
package data.BitSet;
import java.io.IOException;
import java.util.Random;
public class Data {
public static void main(String[] args)throws Exception {
test();
}
/**
* 求反交集
* @throws IOException
*/
public static void test() throws IOException {
int[] randomNums = new int[10000000];
Random random = new Random();
for (int i = 0, length = randomNums.length; i < length; i++) {
randomNums[i] = random.nextInt(10000000);
}
long start = System.currentTimeMillis();
boolean[] bitArray = new boolean[10000000];
for (int i = 0, length = randomNums.length; i < length; i++) {
bitArray[randomNums[i]] = true;
}
for (int i = 0, length = bitArray.length; i < length; i++) {
if (bitArray[i]) {
continue;
}
//System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("Spend milliseconds: " + (end - start));
}
}
import java.io.IOException;
import java.util.Random;
public class Data {
public static void main(String[] args)throws Exception {
test();
}
/**
* 求反交集
* @throws IOException
*/
public static void test() throws IOException {
int[] randomNums = new int[10000000];
Random random = new Random();
for (int i = 0, length = randomNums.length; i < length; i++) {
randomNums[i] = random.nextInt(10000000);
}
long start = System.currentTimeMillis();
boolean[] bitArray = new boolean[10000000];
for (int i = 0, length = randomNums.length; i < length; i++) {
bitArray[randomNums[i]] = true;
}
for (int i = 0, length = bitArray.length; i < length; i++) {
if (bitArray[i]) {
continue;
}
//System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("Spend milliseconds: " + (end - start));
}
}
相关文章推荐
- HDFS工作原理
- Java内存与垃圾回收调优
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
- Java 并发之 ConcurrentSkipListMap 简述
- Junit hamcrest使用
- spring 动态注册数据源
- Mahout中数据的存储方式
- HttpClient4.3 例子
- ElasticSearch导入测试
- 本地模式与StormSubmitter的对比
- mmseg 同义词分析器 SolrSynonymParser
- 布式搜索elasticsearch 中文分词集成
- Spring Bean 初始化过程
- Maven导出工程依赖的jar包
- jactor
- java 线程池详解
- 协同过滤推荐算法在MapReduce与Spark上实现对比
- lucene4.7 分词器(三)
- elasticsearch的实现全文检索
- 详解Java GC的工作原理