编程面试的10大算法概念汇总
編程面試的10大算法概念匯總
嘿,第一次翻譯文章,在ProgramCreek看到的,原文章名為Top 10 Algorithms for Coding Interview, 對于我這個明年即將直奔BAT(目前想法,以后不一定)但基礎知識又差的小程序媛而言有點價值,就拿來翻譯了下,并發表在伯樂在線。文章本身只是總結介紹簡單概念,起個拋磚引玉的作用,具體深入了解需要自己慢慢學習,關鍵是文章中及末尾給出出了很多非常優秀的參考資料,可以繼續深入掌握。下面是正文,有啥想吐槽的請直接評論在下面。
正文:
以下是在編程面試中排名前10的算法相關的概念,我會通過一些簡單的例子來闡述這些概念。由于完全掌握這些概念需要更多的努力,因此這份列表只是作為一個介紹。本文將從Java的角度看問題,包含下面的這些概念:
1. 字符串
2. 鏈表
3. 樹
4. 圖
5. 排序
6. 遞歸 vs. 迭代
7. 動態規劃
8. 位操作
9. 概率問題
10. 排列組合
1. 字符串
如果IDE沒有代碼自動補全功能,所以你應該記住下面的這些方法。
| 1 2 3 4 5 6 | toCharArray() // 獲得字符串對應的char數組 Arrays.sort()? // 數組排序 Arrays.toString(char[] a) // 數組轉成字符串 charAt(int?x) // 獲得某個索引處的字符 length() // 字符串長度 length // 數組大小 |
2. 鏈表
在Java中,鏈表的實現非常簡單,每個節點Node都有一個值val和指向下個節點的鏈接next。
| 1 2 3 4 5 6 7 8 9 10 | <span style="font-size: 15px;">classNode { ????intval; ????Node next; ?? ????Node(intx) { ????????val = x; ????????next =?null; ????} } </span> |
鏈表兩個著名的應用是棧Stack和隊列Queue。
棧:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class?Stack{ ????Node top; ? ????public?Node peek(){ ????????if(top != null){ ????????????return?top; ????????} ? ????????return?null; ????} ? ????public?Node pop(){ ????????if(top == null){ ????????????return?null; ????????}else{ ????????????Node temp = new?Node(top.val); ????????????top = top.next; ????????????return?temp;??? ????????} ????} ? ????public?void?push(Node n){ ????????if(n != null){ ????????????n.next = top; ????????????top = n; ????????} ????} } |
隊列:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class?Queue{ ????Node first, last; ? ????public?void?enqueue(Node n){ ????????if(first == null){ ????????????first = n; ????????????last = first; ????????}else{ ????????????last.next = n; ????????????last = n; ????????} ????} ? ????public?Node dequeue(){ ????????if(first == null){ ????????????return?null; ????????}else{ ????????????Node temp = new?Node(first.val); ????????????first = first.next; ????????????return?temp; ????????}?? ????} } |
3. 樹
這里的樹通常是指二叉樹,每個節點都包含一個左孩子節點和右孩子節點,像下面這樣:
| 1 2 3 4 5 | class?TreeNode{ ????int?value; ????TreeNode left; ????TreeNode right; } |
下面是與樹相關的一些概念:
譯者注:完美二叉樹也隱約稱為完全二叉樹。完美二叉樹的一個例子是一個人在給定深度的祖先圖,因為每個人都一定有兩個生父母。完全二叉樹可以看成是可以有若干額外向左靠的葉子節點的完美二叉樹。疑問:完美二叉樹和滿二叉樹的區別?(參考:http://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html)
4. 圖
圖相關的問題主要集中在深度優先搜索(depth first search)和廣度優先搜索(breath first search)。
下面是一個簡單的圖廣度優先搜索的實現。
1) 定義GraphNode
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class?GraphNode{ ????int?val; ????GraphNode next; ????GraphNode[] neighbors; ????boolean visited; ? ????GraphNode(int?x) { ????????val = x; ????} ? ????GraphNode(int?x, GraphNode[] n){ ????????val = x; ????????neighbors = n; ????} ? ????public?String?toString(){ ????????return?"value: "+ this.val; ????} } |
2) 定義一個隊列Queue
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class?Queue{ ????GraphNode first, last; ? ????public?void?enqueue(GraphNode n){ ????????if(first == null){ ????????????first = n; ????????????last = first; ????????}else{ ????????????last.next = n; ????????????last = n; ????????} ????} ? ????public?GraphNode dequeue(){ ????????if(first == null){ ????????????return?null; ????????}else{ ????????????GraphNode temp = new?GraphNode(first.val, first.neighbors); ????????????first = first.next; ????????????return?temp; ????????}?? ????} } |
3) 用隊列Queue實現廣度優先搜索
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public?class?GraphTest { ? ????public?static?void?main(String[] args) { ????????GraphNode n1 = new?GraphNode(1); ????????GraphNode n2 = new?GraphNode(2); ????????GraphNode n3 = new?GraphNode(3); ????????GraphNode n4 = new?GraphNode(4); ????????GraphNode n5 = new?GraphNode(5); ? ????????n1.neighbors = new?GraphNode[]{n2,n3,n5}; ????????n2.neighbors = new?GraphNode[]{n1,n4}; ????????n3.neighbors = new?GraphNode[]{n1,n4,n5}; ????????n4.neighbors = new?GraphNode[]{n2,n3,n5}; ????????n5.neighbors = new?GraphNode[]{n1,n3,n4}; ? ????????breathFirstSearch(n1, 5); ????} ? ????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); ????????????????} ????????????} ????????} ????} } |
| 1 2 | value: 2 value: 3 value: 5 Find value: 5 value: 4 |
5. 排序
下面是不同排序算法的時間復雜度,你可以去wiki看一下這些算法的基本思想。
| Algorithm | Average Time | Worst Time | Space |
| 冒泡排序 | n^2 | n^2 | 1 |
| 選擇排序 | n^2 | n^2 | 1 |
| Counting Sort | n+k | n+k | n+k |
| Insertion sort | n^2 | n^2 | ? |
| Quick sort | n log(n) | n^2 | ? |
| Merge sort | n log(n) | n log(n) | depends |
另外,這里有一些實現/演示::?Counting sort、Mergesort、?Quicksort、?InsertionSort。
- 《視覺直觀感受 7 種常用的排序算法》
- 《視頻: 6分鐘演示15種排序算法》
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;
| 1 2 3 4 5 | public?static?int?f(int?n){ ????if(n <= 2) return?n; ????int?x = f(n-1) + f(n-2); ????return?x; } |
遞歸方法的時間復雜度是n的指數級,因為有很多冗余的計算,如下:
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)
直接的想法是將遞歸轉換為迭代:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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. 動態規劃
動態規劃是解決下面這些性質類問題的技術:
爬臺階問題完全符合上面的四條性質,因此可以用動態規劃法來解決。
| 1 2 3 4 5 6 7 8 9 10 11 12 | 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計數并從右邊開始)
| 1 2 3 4 5 6 7 8 | 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. 概率問題
解決概率相關的問題通常需要很好的規劃了解問題(formatting the problem),這里剛好有一個這類問題的簡單例子:
一個房間里有50個人,那么至少有兩個人生日相同的概率是多少?(忽略閏年的事實,也就是一年365天)
計算某些事情的概率很多時候都可以轉換成先計算其相對面。在這個例子里,我們可以計算所有人生日都互不相同的概率,也就 是:365/365 * 364/365 * 363/365 * … * (365-49)/365,這樣至少兩個人生日相同的概率就是1 – 這個值。
| 1 2 3 4 5 6 7 8 9 | 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. 排列組合
組合和排列的區別在于次序是否關鍵。
如果你有任何問題請在下面評論。
參考/推薦資料:
1.?Binary tree
2.?Introduction to Dynamic Programming
3.?UTSA Dynamic Programming slides
4.?Birthday paradox
5. Cracking the Coding Interview: 150 Programming Interview Questions and Solutions, Gayle Laakmann McDowell
出處:http://www.cnblogs.com/lanxuezaipiao/p/3447757.html
總結
以上是生活随笔為你收集整理的编程面试的10大算法概念汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Coursera公开课笔记: 斯坦福大学
- 下一篇: 一步步教你Hadoop多节点集群安装配置