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 )
=========================================================================
select a.员工编号ID,a.姓名Name,a.年龄Age,b.部门名称Name
from Employee a,Department b where a.DeptID=b.ID and b.Name='软件开发部'
and a.Age<avg(a.Age)
-------------------------------------------
select a.Id,a.Name,a.DeptID,a.Age from Employee a ,Department b where a.DeptID=b.ID and b.Name='软件开发部门' and a.Age<(a.Age);
------------------------------------------------
select a.员工编号ID,a.姓名Name,a.年龄Age,b.部门名称Name
from Employee a,Department b where a.DeptID=b.ID and b.Name='软件开发部'
and a.Age<avg(a.Age)
==================================
算法
                  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+"次");
}
------------------------------------------------

效率提高
private static void test6() {
String[] array = {"1","1","1","1","1","1","1","1","1","1"};
List outValue = new ArrayList();
int length = array.length - 1;
int count = 0;
String element = array[length];
while (true) {
int tempCount = 0;
if (element != null) {
for (int i = length; i >= 0; i--) {
if (element == array[i]) {
tempCount++;
array[i]=null;
}
}
if (tempCount > count) {
count = tempCount;
outValue.clear();
outValue.add(element);
} else if (tempCount == count) {
outValue.add(element);
}
if (length >= 0) {
element = array[length];
}
} else {
--length;
if (length < 0) {
 break;
}
element = array[length];
continue;
}

}
-----------------------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值