英文版的面試10大算法彙總日最高訪問量已高達4,318次。這說明總結程序員面試算法有實際意義,比讀算法書更有效。下面是中文版的10大算法彙總+有表明性的題目彙總。這些概念是專門爲面試準備的,由於平常編程中咱們不多會本身去寫一個鏈表或者作一個圖,也不會常用沒有效率的遞歸。java
如下用Java角度解釋面試常見的算法和數據結構:字符串,鏈表,樹,圖,排序,遞歸 vs. 迭代,動態規劃,位操做,機率問題,排列組合,以及一些須要尋找規律的題目。程序員
1. 字符串和數組面試
首先須要注意的是和C++不一樣,Java字符串不是char數組。沒有IDE代碼自動補全功能,應該記住下面的這些經常使用的方法。算法
toCharArray() //得到字符串對應的char數組 Arrays.sort() //數組排序 Arrays.toString(char[] a) //數組轉成字符串 charAt(int x) //得到某個索引處的字符 length() //字符串長度 length //數組大小 substring(int beginIndex) substring(int beginIndex, int endIndex) Integer.valueOf() //string to integer String.valueOf() /integer to string
字符串和數組自己很簡單,可是相關的題目須要更復雜的算法來解決。好比說動態規劃,搜索,等等。編程
經典題目: Evaluate Reverse Polish Notation, Longest Palindromic Substring, Word Break, Word Ladder.數組
2. 鏈表數據結構
在Java中,鏈表的實現很是簡單,每一個節點Node都有一個值val和指向下個節點的連接next。dom
class Node { int val; Node next; Node(int x) { val = x; next = null; } }
鏈表兩個著名的應用是棧Stack和隊列Queue。在Java標準庫都都有實現,一個是Stack,另外一個是LinkedList(Queue是它實現的接口)。ide
經典題目: Add Two Numbers, Reorder List, Linked List Cycle, Copy List with Random Pointer.
3. 樹
這裏的樹一般是指二叉樹,每一個節點都包含一個左孩子節點和右孩子節點,像下面這樣:
class TreeNode{ int value; TreeNode left; TreeNode right; }
下面是與樹相關的一些概念:
二叉搜索樹:左結點 <= 中結點 <= 右結點
平衡 vs. 非平衡:平衡二叉樹中,每一個節點的左右子樹的深度相差至多爲1(1或0)。
滿二叉樹(Full Binary Tree):除葉子節點覺得的每一個節點都有兩個孩子。
完美二叉樹(Perfect Binary Tree):是具備下列性質的滿二叉樹:全部的葉子節點都有相同的深度或處在同一層次,且每一個父節點都必須有兩個孩子。
徹底二叉樹(Complete Binary Tree):二叉樹中,可能除了最後一個,每一層都被徹底填滿,且全部節點都必須儘量想左靠。
經典題目:Binary Tree Preorder Traversal , Binary Tree Inorder Traversal, Binary Tree Postorder Traversal,Word Ladder.
4. 圖
圖相關的問題主要集中在深度優先搜索(depth first search)和廣度優先搜索(breath first search)。深度優先搜索很簡單,廣度優先要注意使用queue. 下面是一個簡單的用隊列Queue實現廣度優先搜索。
public class GraphTest { public static void breathFirstSearch(GraphNode root, int x){ if(root.val == x) System.out.println("find in root"); Queue queue = new Queue(); root.visited = true; queue.enqueue(root); while(queue.first != null){ GraphNode c = (GraphNode) queue.dequeue(); for(GraphNode n: c.neighbors){ if(!n.visited){ System.out.print(n + " "); n.visited = true; if(n.val == x) System.out.println("Find "+n); queue.enqueue(n); } } } } }
5. 排序
下面是不一樣排序算法的時間複雜度,你能夠去wiki看一下這些算法的基本思想。
Algorithm | Average Time | Worst Time | Space |
冒泡排序(Bubble sort) | n^2 | n^2 | 1 |
選擇排序(Selection sort) | n^2 | n^2 | 1 |
插入排序(Insertion sort) | n^2 | n^2 | |
快速排序(Quick sort) | n log(n) | n^2 | |
歸併排序(Merge sort) | n log(n) | n log(n) | depends |
* 另外還有BinSort, RadixSort和CountSort 三種比較特殊的排序。
經典題目: Mergesort, Quicksort, InsertionSort.
6. 遞歸 vs. 迭代
對程序員來講,遞歸應該是一個與生俱來的思想(a built-in thought),能夠經過一個簡單的例子來講明。
問題:
有n步臺階,一次只能上1步或2步,共有多少種走法。
步驟1:找到走完前n步臺階和前n-1步臺階之間的關係。
爲了走完n步臺階,只有兩種方法:從n-1步臺階爬1步走到或從n-2步臺階處爬2步走到。若是f(n)是爬到第n步臺階的方法數,那麼f(n) = f(n-1) + f(n-2)。
步驟2: 確保開始條件是正確的。
f(0) = 0;
f(1) = 1;
public static int f(int n){ if(n <= 2) return n; int x = f(n-1) + f(n-2); return x; }
遞歸方法的時間複雜度是指數級,由於有不少冗餘的計算:
f(5) f(4) + f(3) f(3) + f(2) + f(2) + f(1) f(2) + f(1) + f(1) + f(0) + f(1) + f(0) + f(1) f(1) + f(0) + f(1) + f(1) + f(0) + f(1) + f(0) + f(1)
直接的想法是將遞歸轉換爲迭代:
public static int f(int n) { if (n <= 2){ return n; } int first = 1, second = 2; int third = 0; for (int i = 3; i <= n; i++) { third = first + second; first = second; second = third; } return third; }
這個例子迭代花費的時間更少,你可能複習一個二者的區別Recursion vs Iteration。
7. 動態規劃
動態規劃是解決下面這些性質類問題的技術:
爬臺階問題徹底符合上面的四條性質,所以能夠用動態規劃法來解決。
public static int[] A = new int[100]; public static int f3(int n) { if (n <= 2) A[n]= n; if(A[n] > 0) return A[n]; else A[n] = f3(n-1) + f3(n-2);//store results so only calculate once! return A[n]; }
8. 位操做
經常使用位操做符:
OR (|) | AND (&) | XOR (^) | Left Shift (<<) | Right Shift (>>) | Not (~) |
1|0=1 | 1&0=0 | 1^0=1 | 0010<<2=1000 | 1100>>2=0011 | ~1=0 |
用一個題目來理解這些操做 -
得到給定數字n的第i位:(i從0計數並從右邊開始)
public static boolean getBit(int num, int i){ int result = num & (1<<i); if(result == 0){ return false; }else{ return true; }
例如,得到數字10的第2位:
i=1, n=10 1<<1= 10 1010&10=10 10 is not 0, so return true;
9. 機率問題
解決機率相關的問題一般須要先分析問題,下面是一個這類問題的簡單例子:
一個房間裏有50我的,那麼至少有兩我的生日相同的機率是多少?(忽略閏年的事實,也就是一年365天)
計算某些事情的機率不少時候均可以轉換成先計算其相對面。在這個例子裏,咱們能夠計算全部人生日都互不相同的機率,也就是:365/365 * 364/365 * 363/365 * … * (365-49)/365,這樣至少兩我的生日相同的機率就是1 – 這個值。
public static double caculateProbability(int n){ double x = 1; for(int i=0; i<n; i++){ x *= (365.0-i)/365.0; } double pro = Math.round((1-x) * 100); return pro/100; }
calculateProbability(50) = 0.97
10. 排列組合
組合和排列的區別在於次序是否關鍵。
11. 其餘類型的題目
主要是不能歸到上面10大類的。須要尋找規律,而後解決問題的。
經典題目: Reverse Integer
更新記錄
算法彙總會不算更新,同時偶爾我會面試Google, Facebook, Amazon, Microsoft等等比較有名的公司,面經也會分享在這裏。歡迎收藏本頁。
微博:http://www.weibo.com/programcreek
12/06/2013 – 添加 「Add Two Numbers」, 「Binary Tree Traversal(pre/in/post-order)」, 「Find Single Number」,」Word Break」,」Reorder List」,」Edit Distance」, 」 Reverse Integer」
12/14/2013 – 添加 「Copy List with Random Pointer」, 「Evaluate Reverse Polish Notation」, 「Word Ladder」.
引用:
1. Binary tree
2. Introduction to Dynamic Programming
3. UTSA Dynamic Programming slides
4. Birthday paradox
5. Cracking the Coding Interview: 150 Programming InterviewQuestions and Solutions, Gayle Laakmann McDowell
5. Counting sort
6. 感謝伯樂在線-敏敏的翻譯
7. Top 10 Algorithms for Coding Interview