Redian新闻
>
career cup 150第五版10.3 中numberOfInts为什么要除以8?
avatar
career cup 150第五版10.3 中numberOfInts为什么要除以8?# JobHunting - 待字闺中
S*C
1
/**
* 10.3 Given an input file with four billion non-negative integers, provide
an algorithm
to generate an integer which is not contained in the file. Assume you have
1 GB of
memory available for this task.
FOLLOW UP
What if you have only 10 M8 of memory? Assume that all the values are
distinct and
we now have no more than one billion non-negative integers.
CC Page 347
*/
import java.io.*;
import java.util.Scanner;
//A bit array (also known as bitmap, bitset, bit string, or bit vector) is
an array data structure that compactly stores bits.
import java.util.BitSet;//try using this data structure
public class BestSolution {
public static int findMissingNumber(){
long numberOfInts = ((long) Integer.MAX_VALUE) + 1;
System.out.println("numberOfInts / 8 = "+ numberOfInts / 8);//
268435456
BitSet bitSet = new BitSet((int) (numberOfInts / 8));//这里为什么要
除以8?不是说要40亿个bits吗?
FileReader fr = null;
BufferedReader br = null;
Scanner in = null;
try {
fr = new FileReader("D:/data/test2.txt");
br = new BufferedReader(fr);
in = new Scanner(br);
while (in.hasNextInt()) {
bitSet.set(in.nextInt());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (in != null)
in.close();
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException e) {
}
}
for (int i = 0; i < numberOfInts; i++)
if (!bitSet.get(i))
return i;
return -1;
}
public static void main(String[] args) throws IOException {
System.out.println("find "+findMissingNumber());
}

}
avatar
s*7
2
应该是typo,估计后面的版本会纠正的
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。