日期:2014-05-20  浏览次数:20726 次

java面试题
1。从n个整数中查找出现频率最高的所有整数(用java实现)
示例:5,5,8,5,3,5,3,3,3,1
中出现频率最高的整数是3和5
    第一道题目我做出来了,问题是算法写的太垃圾了,
    请你们把你们的算法写出来参考一下

   


还有一题数据库
2。     公司员工管理数据库中有俩个基本表
员工Employee(员工编号ID,姓名Name,所在部门DeptID,年龄Age);
部门Department(部门编号ID,部门名称Name,职能Function,地址Addr);
Employee   表中的DeptID和Department表中的ID关联;
    请检索年龄低于员工平均年龄的所有 "软件开发部 "员工(用sql语句实现,计算平均值的sql函数为AVG)
select   *   from  
employee   e   inner   join   department   d
on   e.id=d.id
and   部门= '软件开发部 '  
and   where年龄 <   (select   avg(年龄)   as   '平均年龄 '   from   employee   )

2道题目是否正确呢,帮我看一下,
请你们把你们的算法写出来参考一下

------解决方案--------------------
http://community.csdn.net/Expert/topic/5600/5600985.xml?temp=.6610224 的算法有问题, 效率不高,而且只能打出一个结果.
我的算法
int[] ar = {5,5,8,5,3,5,3,3,3,1};
int[] fr = new int[ar.length];
int top = 0;
int i, j;

for (i = 0; i < fr.length; i++) {
j = i - 1;
while (j > = 0) {
if (ar[j] == ar[i])
break;
j--;
}

if (j < 0)
fr[i] = 1;
else
fr[i] = fr[j] + 1;

if (fr[i] > top)
top = fr[i];
}

for (i = 0; i < fr.length; i++) {
if (fr[i] == top)
System.out.println( " " + ar[i] + " " + top);
}
}
------解决方案--------------------
大大的降低了循环次数。
private static void test5() {
String[] array = { "5 ", "5 ", "8 ", "5 ", "3 ", "5 ", "3 ", "3 ", "3 ", "1 "};
List list= Arrays.asList(array);
list = new ArrayList(list);
List outValue = new ArrayList();
int count = 0;
while (list.size()-1 > 0) {
String element = (String) list.get(list.size()-1);
int tempCount = 0;
for (int j = list.size()-1; j > = 0; j--) {
if (element.equals(list.get(j))) {
tempCount++;
list.remove(j);
}
}
if (tempCount > count) {
count = tempCount;
outValue.clear();
outValue.add(element);
} else if (tempCount == count) {
outValue.add(element);
}
}
System.out.println( "出现频率最高的是: "+outValue+ "\n出现了: "+count+ "次 ");
}