一道牛逼的java面试题

IT-麦迪 2012-02-23 05:56:32
一个数组长度是10万 存的是数字而且无序 其中有两个相同的数字 用最优算法求出这两个数来
例如arr[0..100000] 其中arr[0]和arr[22]相同
...全文
650 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
czmchen 2012-02-29
  • 打赏
  • 举报
回复
这个简单,源码送上



package com.comtop.test;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author czm
* @since JDK1.5
* @history 2012-2-29 czm 新建
*/
public class BigDataRepeatTest {
private static final int[] data = new int[100000];


public static void main(String[] args) {
//初始化data
for(int i = 0;i<data.length;i++){
data[i] = i;
}
data[99999] = 0;

long startTime = System.currentTimeMillis();
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0;i<data.length;i++){
if(map.get(data[i])!=null){
System.out.println("第"+i+"个重复,重复值为"+data[i]);
System.out.println("get you:"+data[i]);
break;
}else{
map.put(data[i], data[i]);
}
}
long endTime = System.currentTimeMillis();
System.out.println("查找"+data.length+"重复数据一共耗费:"+(endTime-startTime)+"毫秒");

}

}


昨日凡阳 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ldh911 的回复:]

第一时间想到的是用HashSet来搞,运气好的话,不需要遍历整个数组,就可以退出循环了。
HashSet<Integer> set = new HashSet<Integer>();
for (a in arr) {
if (set.contains(a)) break; // 找到啦
set.add(a);
}
[/Quote]

你没写完吧
bawgiitx 2012-02-28
  • 打赏
  • 举报
回复

//时间和空间,看要那个了
public static void main(String[] args) {
// TODO code application logic here
int data[] = new int[100000];
for (int i = 0; i < data.length; i++) {
data[i] = i + 1;
}
data[22] = data[0];
try {
System.out.println(sf(data));
} catch (Exception ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
static int[] key = new int[]{0b1, 0b10, 0b100, 0b1000, 0b10000, 0b100000, 0b1000000, 0b10000000};

static int sf(int data[]) throws Exception {
int len = 256 * 1024 * 1024;
//分页,正和负各2g bit,共要4gbit/8=512gbyte
byte[] p1 = new byte[len];
byte[] p2 = new byte[len];
int index;
for (int d : data) {
index = d & 0x7fff;
if (d < 0) {
if ((p2[index / 8] & key[index % 8]) > 0x0) {
return index;
} else {
p2[index / 8] |= key[index % 8];
}
} else {
if ((p1[index / 8] & key[index % 8]) > 0x0) {
return index;
} else {
p1[index / 8] |= key[index % 8];
}
}
}
throw new Exception();
}
bawgiitx 2012-02-28
  • 打赏
  • 举报
回复

//还有比这个更快的么
boolean[]finds=new boolean[4g];
for(int i=0;i<data.length;i++){
if(finds[i]){
System.out.printf("same:"+i);
break;
}else{
finds[i]=true;
}
}
shusheng1997 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 bawgiitx 的回复:]
Java code


//时间和空间,看要那个了
public static void main(String[] args) {
// TODO code application logic here
int data[] = new int[100000];
for (int i = 0; i < data.length;……
[/Quote]
牛人啊
  • 打赏
  • 举报
回复
排个序就很容易比了,10万,不如直接扔到数据库里,group by一下
yanhandle 2012-02-27
  • 打赏
  • 举报
回复
你这个方法看的应该是编程之美的方法吧,使用bit数组来做,其实也只不过是替换了hashmap自有的hash算法,使所有的hash值不一样,保证了查询的o(1),但是你声明的数组要多大,是在java里是2~32-1,如果是long的话就更麻烦,以如此大的空间换取那点效率不值得。

[Quote=引用 11 楼 gukuitian 的回复:]

楼上的同学没注意,你把数组中的数据放入map的时候,也是o(n)
引用 10 楼 yanhandle 的回复:
可以用hashmap,以值为key,以索引为值,hashset底层也是由hashmap来实现。至于效率,hashmap的查询效率是o(1),用一楼的方法效率就是0(n),应该最快了吧


有个挻怪异的方法,new个同长的字符串数组a,遍历原数组,
if(a[arr[i]].……
[/Quote]
wcl_friend 2012-02-24
  • 打赏
  • 举报
回复
感觉楼上的方法可行,学习了,hashSet确实好用,但不知道谁还有更好的办法,关注中。。。。
  • 打赏
  • 举报
回复
method 1:
List<Integer> list = new ArrayList<Integer>();
for(int a : arr){

if(list.contains(a)) break;
list.add(a);

}






gukuitian 2012-02-24
  • 打赏
  • 举报
回复
要求是新数组的长度大于原数组的最大值,
gukuitian 2012-02-24
  • 打赏
  • 举报
回复
楼上的同学没注意,你把数组中的数据放入map的时候,也是o(n)
[Quote=引用 10 楼 yanhandle 的回复:]
可以用hashmap,以值为key,以索引为值,hashset底层也是由hashmap来实现。至于效率,hashmap的查询效率是o(1),用一楼的方法效率就是0(n),应该最快了吧
[/Quote]

有个挻怪异的方法,new个同长的字符串数组a,遍历原数组,
if(a[arr[i]].equals(""))
a[arr[i]]=i+"-"+arr[i];
else
System.out.println(i+"-"+a[arr[i]]);
yanhandle 2012-02-24
  • 打赏
  • 举报
回复
可以用hashmap,以值为key,以索引为值,hashset底层也是由hashmap来实现。至于效率,hashmap的查询效率是o(1),用一楼的方法效率就是0(n),应该最快了吧
gukuitian 2012-02-24
  • 打赏
  • 举报
回复
5楼被吃了。
gukuitian 2012-02-24
  • 打赏
  • 举报
回复
Set 能找到值,但找不到对应的索引啊。
吸尘器 2012-02-24
  • 打赏
  • 举报
回复
而且我估计,如果这个是面试题目的话,肯定还有要求,不用java现有已封装好的方法
吸尘器 2012-02-24
  • 打赏
  • 举报
回复
不知道楼上的hashset效率怎么样,建议借用冒泡排序的方法进行查找应该效率更高些吧
gukuitian 2012-02-24
  • 打赏
  • 举报
回复
Set 找到了相当的数,可没有对应的索引啊
a597926661 2012-02-24
  • 打赏
  • 举报
回复
跟数组去除冗余挺像啊 这个的方法一搜一堆 3楼的么看懂 关注中 寻求最佳方法。。。
dsfdfdsgfgg 2012-02-24
  • 打赏
  • 举报
回复
int result=0;
for(int i: arr) {
result=i ^ result;
}
System.out.println(result);
MiceRice 2012-02-23
  • 打赏
  • 举报
回复
第一时间想到的是用HashSet来搞,运气好的话,不需要遍历整个数组,就可以退出循环了。
HashSet<Integer> set = new HashSet<Integer>();
for (a in arr) {
if (set.contains(a)) break; // 找到啦
set.add(a);
}

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧