百度数据挖掘部门
自我介绍,扯了一些项目方面的东西,就是简历上的,然后开始写代码,最后问你有没有什么问题。
题目如下:
(1)用两个栈实现一个队列(优化后解)
public class QueueImplementByTwoStacks {
Stack<Integer> a=new Stack<Integer>();
Stack<Integer> b=new Stack<Integer>();
public void add(int num){
a.push(num);
}
public int pop(){
if(!b.empty()){
return b.pop();
}
else{
while(!a.empty()){
b.push(a.pop());
}
return b.pop();
}
}
public static void main(String[] args){
QueueImplementByTwoStacks queue=new QueueImplementByTwoStacks();
queue.add(8);
queue.add(9);
System.out.print(""+queue.pop());
}
}
(2)矩阵乘法
public class MultiplyMatrix {
public int[][] multiply(int[][] a,int[][] b){
int[][] result=new int[a.length][b[0].length];
for(int i=0;i<a.length;i++){
for(int j=0;j<b[0].length;j++){
for(int k=0;k<a[0].length;k++){
result[i][j]+=a[i][k]*b[k][j];
}
}
}
return result;
}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/