【interview】Microsoft面經

~~收集的面經~~html

1. 實現hashtable的put 和get操做node

參考:https://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E7%8E%B0/git

put:github

  1. 對key的hashCode()作hash,而後再計算index;
  2. 若是沒碰撞直接放到bucket裏;
  3. 若是碰撞了,以鏈表的形式存在buckets後;
  4. 若是碰撞致使鏈表過長(大於等於TREEIFY_THRESHOLD),就把鏈表轉換成紅黑樹;
  5. 若是節點已經存在就替換old value(保證key的惟一性)
  6. 若是bucket滿了(超過load factor*current capacity),就要resize

get:spa

  1. bucket裏的第一個節點,直接命中;
  2. 若是有衝突,則經過key.equals(k)去查找對應的entry
    若爲樹,則在樹中經過key.equals(k)查找,O(logn);
    若爲鏈表,則在鏈表中經過key.equals(k)查找,O(n)

2. 給定一個8*8的棋盤,一個起始位置si,sj, 一個終止位置ei,ej,求問馬從起始位置到終止位置最少須要多少步。.net

附:八皇后問題rest

 https://blog.csdn.net/friendbkf/article/details/49892039code

https://www.cnblogs.com/xinghuan/p/6061824.htmlhtm

3. 給定一棵二叉樹,求這顆二叉樹最大子路徑和,包括橫跨根結點的路徑blog

https://blog.csdn.net/feeltouch/article/details/78511214

 

public class Solution {
    
    private int max = Integer.MIN_VALUE;
    
    public int maxPathSum(TreeNode root) {
        helper(root);
        return max;
    }
    
    public int helper(TreeNode root) {
        if(root == null) return 0;
        int left = helper(root.left);
        int right = helper(root.right);
        //鏈接父節點的最大路徑是1、2、四這三種狀況的最大值
        int currSum = Math.max(Math.max(left + root.val, right + root.val), root.val);
        //當前節點的最大路徑是1、2、3、四這四種狀況的最大值
        int currMax = Math.max(currSum, left + right + root.val);
        //用當前最大來更新全局最大
        max = Math.max(currMax, max);
        return currSum;
    }
}

 

 

 

4. 每k個反轉單鏈表。

https://blog.csdn.net/beiyetengqing/article/details/7596707

 

public static Node reverse (Node head, int k) {
    Node current = head;
    Node next = null;
    Node prev = null;
    int count = 0;   
    
    /*reverse first k nodes of the linked list */
    while (current != null && count < k) {
       next  = current.next;
       current.next = prev;
       prev = current;
       current = next;
       count++;
    }
 
    /* next is now a pointer to (k+1)th node
       Recursively call for the list starting from current.
       And make rest of the list as next of first node */
    if(next !=  null) {
        head.next = reverse(next, k); 
    }
 
    /* prev is new head of the input list */
    return prev;
}

 

 

 

Struct btree{ Int value; Btree*l; Btree*r; } int maxn=-1; int find_max(Btree*root){ if(!root) return 0; int l = find_max(root->l) int r = find_max(root->r) int m = max(i,j)+root->value; int m2 if(m>maxn) maxn=m; return m;  }                      
相關文章
相關標籤/搜索