一亿个数字判断其中相同数字的办法
一亿个数字判断其中相同数字的办法
package com.my.util;
//http://blog.csdn.net/yjflinchongpublic class Test {
int fnum = 21000000;
public static void main(String[] args) {
Test t = new Test();
t.find();
}
public void find() {
int total = 100000000;
int size = total%32==0?total/32:total/32+1;
int [] mBits=new int[size];
for(int i=0;i<total;i++) {
int num = getNum(i);
if(get(mBits,num)) {
System.out.println(num);
break;
}
set1(mBits,num);
}
}
//http://blog.csdn.net/yjflinchong
public int getNum(int i) {
//设定模拟重复的那个数字 fnum
if(i==(fnum+1)){
i--;
}
return i;
}
public void set1(int [] mBits, int pos) {
int index = ( int )Math.floor(pos/32f);
mBits[index] = mBits[index] | (1 <<(31-pos%32 ));
}
public boolean get(int [] mBits, int pos){
int index = ( int )Math.floor(pos/32f);
return mBits[index] == (mBits[index] | 1 <<(31-pos% 32 ));
}
}