面試總結-百度(1)

李博Garvin發表於2015-03-25

百度資料探勘部門

       自我介紹,扯了一些專案方面的東西,就是簡歷上的,然後開始寫程式碼,最後問你有沒有什麼問題。

題目如下:

(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

******************************************/