编程面试的10大算法概念汇总

开发 项目管理 算法
以下是在编程面试中排名前10的算法相关的概念,我会通过一些简单的例子来阐述这些概念。由于完全掌握这些概念需要更多的努力,因此这份列表只是作为一个介绍。

以下是在编程面试中排名前10的算法相关的概念,我会通过一些简单的例子来阐述这些概念。由于完全掌握这些概念需要更多的努力,因此这份列表只是作为一个介绍。本文将从Java的角度看问题,包含下面的这些概念:

1. 字符串

2. 链表

3. 树

4. 图

5. 排序

6. 递归 vs. 迭代

7. 动态规划

8. 位操作

9. 概率问题

10. 排列组合

1. 字符串

如果IDE没有代码自动补全功能,所以你应该记住下面的这些方法。

  1. toCharyArray() // 获得字符串对应的char数组 
  2. Arrays.sort()  // 数组排序 
  3. Arrays.toString(char[] a) // 数组转成字符串 
  4. charAt(int x) // 获得某个索引处的字符 
  5. length() // 字符串长度 
  6. length // 数组大小 

2. 链表

在Java中,链表的实现非常简单,每个节点Node都有一个值val和指向下个节点的链接next。

  1. class Node { 
  2.     int val; 
  3.     Node next; 
  4.   
  5.     Node(int x) { 
  6.         val = x; 
  7.         next = null
  8.     } 

链表两个著名的应用是栈Stack和队列Queue。

栈:

  1. class Stack{ 
  2.     Node top; 
  3.   
  4.     public Node peek(){ 
  5.         if(top != null){ 
  6.             return top; 
  7.         } 
  8.   
  9.         return null
  10.     } 
  11.   
  12.     public Node pop(){ 
  13.         if(top == null){ 
  14.             return null
  15.         }else
  16.             Node temp = new Node(top.val); 
  17.             top = top.next; 
  18.             return temp;    
  19.         } 
  20.     } 
  21.   
  22.     public void push(Node n){ 
  23.         if(n != null){ 
  24.             n.next = top; 
  25.             top = n; 
  26.         } 
  27.     } 

队列:

  1. class Queue{ 
  2.     Node first, last; 
  3.   
  4.     public void enqueue(Node n){ 
  5.         if(first == null){ 
  6.             first = n; 
  7.             last = first; 
  8.         }else
  9.             last.next = n; 
  10.             last = n; 
  11.         } 
  12.     } 
  13.   
  14.     public Node dequeue(){ 
  15.         if(first == null){ 
  16.             return null
  17.         }else
  18.             Node temp = new Node(first.val); 
  19.             first = first.next; 
  20.             return temp; 
  21.         }   
  22.     } 

3. 树

这里的树通常是指二叉树,每个节点都包含一个左孩子节点和右孩子节点,像下面这样:

  1. class TreeNode{ 
  2.     int value; 
  3.     TreeNode left; 
  4.     TreeNode right; 

下面是与树相关的一些概念:

  1. 平衡 vs. 非平衡:平衡二叉树中,每个节点的左右子树的深度相差至多为1(1或0)。
  2. 满二叉树(Full Binary Tree):除叶子节点以为的每个节点都有两个孩子。
  3. 完美二叉树(Perfect Binary Tree):是具有下列性质的满二叉树:所有的叶子节点都有相同的深度或处在同一层次,且每个父节点都必须有两个孩子。
  4. 完全二叉树(Complete Binary Tree):二叉树中,可能除了最后一个,每一层都被完全填满,且所有节点都必须尽可能想左靠。

译者注:完美二叉树也隐约称为完全二叉树。完美二叉树的一个例子是一个人在给定深度的祖先图,因为每个人都一定有两个生父母。完全二叉树可以看成是可以有若干额外向左靠的叶子节点的完美二叉树。疑问:完美二叉树和满二叉树的区别?(参考:http://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html

#p#

4. 图

图相关的问题主要集中在深度优先搜索(depth first search)和广度优先搜索(breath first search)。

下面是一个简单的图广度优先搜索的实现。

1) 定义GraphNode

  1. class GraphNode{ 
  2.     int val; 
  3.     GraphNode next; 
  4.     GraphNode[] neighbors; 
  5.     boolean visited; 
  6.   
  7.     GraphNode(int x) { 
  8.         val = x; 
  9.     } 
  10.   
  11.     GraphNode(int x, GraphNode[] n){ 
  12.         val = x; 
  13.         neighbors = n; 
  14.     } 
  15.   
  16.     public String toString(){ 
  17.         return "value: "this.val; 
  18.     } 

2) 定义一个队列Queue

  1. class Queue{ 
  2.     GraphNode first, last; 
  3.   
  4.     public void enqueue(GraphNode n){ 
  5.         if(first == null){ 
  6.             first = n; 
  7.             last = first; 
  8.         }else
  9.             last.next = n; 
  10.             last = n; 
  11.         } 
  12.     } 
  13.   
  14.     public GraphNode dequeue(){ 
  15.         if(first == null){ 
  16.             return null
  17.         }else
  18.             GraphNode temp = new GraphNode(first.val, first.neighbors); 
  19.             first = first.next; 
  20.             return temp; 
  21.         }   
  22.     } 

3) 用队列Queue实现广度优先搜索

  1. public class GraphTest { 
  2.   
  3.     public static void main(String[] args) { 
  4.         GraphNode n1 = new GraphNode(1); 
  5.         GraphNode n2 = new GraphNode(2); 
  6.         GraphNode n3 = new GraphNode(3); 
  7.         GraphNode n4 = new GraphNode(4); 
  8.         GraphNode n5 = new GraphNode(5); 
  9.   
  10.         n1.neighbors = new GraphNode[]{n2,n3,n5}; 
  11.         n2.neighbors = new GraphNode[]{n1,n4}; 
  12.         n3.neighbors = new GraphNode[]{n1,n4,n5}; 
  13.         n4.neighbors = new GraphNode[]{n2,n3,n5}; 
  14.         n5.neighbors = new GraphNode[]{n1,n3,n4}; 
  15.   
  16.         breathFirstSearch(n1, 5); 
  17.     } 
  18.   
  19.     public static void breathFirstSearch(GraphNode root, int x){ 
  20.         if(root.val == x) 
  21.             System.out.println("find in root"); 
  22.   
  23.         Queue queue = new Queue(); 
  24.         root.visited = true
  25.         queue.enqueue(root); 
  26.   
  27.         while(queue.first != null){ 
  28.             GraphNode c = (GraphNode) queue.dequeue(); 
  29.             for(GraphNode n: c.neighbors){ 
  30.   
  31.                 if(!n.visited){ 
  32.                     System.out.print(n + " "); 
  33.                     n.visited = true
  34.                     if(n.val == x) 
  35.                         System.out.println("Find "+n); 
  36.                     queue.enqueue(n); 
  37.                 } 
  38.             } 
  39.         } 
  40.     } 

Output:

  1. value: 2 value: 3 value: 5 Find value: 5 
  2. 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种排序算法》

#p#

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. public static int f(int n){ 
  2.     if(n <= 2return n; 
  3.     int x = f(n-1) + f(n-2); 
  4.     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. public static int f(int n) { 
  2.   
  3.     if (n <= 2){ 
  4.         return n; 
  5.     } 
  6.   
  7.     int first = 1, second = 2
  8.     int third = 0
  9.   
  10.     for (int i = 3; i <= n; i++) { 
  11.         third = first + second; 
  12.         first = second; 
  13.         second = third; 
  14.     } 
  15.   
  16.     return third; 

对这个例子而言,迭代花费的时间更少,你可能也想看看Recursion vs Iteration。

7. 动态规划

动态规划是解决下面这些性质类问题的技术:

一个问题可以通过更小子问题的解决方法来解决(译者注:即问题的最优解包含了其子问题的最优解,也就是最优子结构性质)。

有些子问题的解可能需要计算多次(译者注:也就是子问题重叠性质)。

子问题的解存储在一张表格里,这样每个子问题只用计算一次。

需要额外的空间以节省时间。

爬台阶问题完全符合上面的四条性质,因此可以用动态规划法来解决。

  1. public static int[] A = new int[100]; 
  2.   
  3. public static int f3(int n) { 
  4.     if (n <= 2
  5.         A[n]= n; 
  6.   
  7.     if(A[n] > 0
  8.         return A[n]; 
  9.     else 
  10.         A[n] = f3(n-1) + f3(n-2);//store results so only calculate once! 
  11.     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. public static boolean getBit(int num, int i){ 
  2.     int result = num & (1<<i); 
  3.   
  4.     if(result == 0){ 
  5.         return false
  6.     }else
  7.         return true
  8.     } 

例如,获得数字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-n/365 + 365-49/365,这样至少两个人生日相同的概率就是1 – 这个值。

  1. public static double caculateProbability(int n){ 
  2.     double x = 1
  3.   
  4.     for(int i=0; i<n; i++){ 
  5.         x *=  (365.0-i)/365.0
  6.     } 
  7.   
  8.     double pro = Math.round((1-x) * 100); 
  9.     return pro/100

calculateProbability(50) = 0.97

10. 排列组合

组合和排列的区别在于次序是否关键。

原文链接:http://www.programcreek.com/2012/11/top-10-algorithms-for-coding-interview/

译文链接:http://blog.jobbole.com/52144/

责任编辑:陈四芳 来源: 伯乐在线
相关推荐

2020-08-16 12:38:32

Python算法编程

2020-09-21 09:36:05

机器学习人工智能计算机

2013-12-17 09:12:04

2011-12-05 10:38:37

Java面试

2015-11-12 11:05:07

java排序算法

2019-03-20 07:50:47

机器学习算法线性回归

2010-08-16 11:04:36

路由算法

2020-10-13 18:14:05

IT编程语言编码

2011-05-26 10:13:03

编程字体

2017-12-28 10:39:23

编程网站编辑

2011-10-09 08:58:11

程序员

2015-09-16 16:04:53

程序员面试技巧

2021-08-25 09:59:00

开发排序代码

2020-10-13 15:11:18

编程语言薪资IT

2010-05-19 18:23:34

2015-08-21 10:18:17

热门编程语言Java

2014-06-03 10:05:13

2014-10-30 15:30:35

编程技术算法

2011-06-22 14:38:14

JAVA

2011-06-22 15:04:28

JAVA
点赞
收藏

51CTO技术栈公众号