分享

面试10大算法汇总

pig2 发表于 2015-2-27 17:38:57 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 3 14779
本帖最后由 pig2 于 2015-2-27 17:38 编辑
问题导读
1.本文列举了哪些算法?
2.你认为这些算法中那些是经常考的内容?
3.树有哪些概念?
4.动态规划是解决哪些性质类问题的技术?





英文版

以下从Java角度解释面试常见的算法和数据结构:字符串,链表,树,图,排序,递归 vs. 迭代,动态规划,位操作,概率问题,排列组合,以及一些需要寻找规律的题目。

1. 字符串和数组

字符串和数组是最常见的面试题目类型,应当分配最大的时间。关于字符串,首先需要注意的是和C++不同,Java字符串不是char数组。没有IDE代码自动补全功能,应该记住下面的这些常用的方法。

  1. toCharArray() //获得字符串对应的char数组
  2. Arrays.sort()  //数组排序
  3. Arrays.toString(char[] a) //数组转成字符串
  4. charAt(int x) //获得某个索引处的字符
  5. length() //字符串长度
  6. length //数组大小
  7. substring(int beginIndex)
  8. substring(int beginIndex, int endIndex)
  9. Integer.valueOf() //string to integer
  10. String.valueOf() /integer to string
复制代码


字符串和数组本身很简单,但是相关的题目需要更复杂的算法来解决。比如说动态规划,搜索,等等。

经典题目:
1) Evaluate Reverse Polish Notation
2) Longest Palindromic Substring
3) Word Break
4) Word Ladder
5) Median of Two Sorted Arrays
6) Regular Expression Matching
7) Merge Intervals
8) Insert Interval
9) Two Sum
9) 3Sum
9) 4Sum
10) 3Sum Closest
11) String to Integer
12) Merge Sorted Array
13) Valid Parentheses
14) Implement strStr()
15) Set Matrix Zeroes
16) Search Insert Position
17) Longest Consecutive Sequence
18) Valid Palindrome
19) Spiral Matrix
20) Search a 2D Matrix
21) Rotate Image
22) Triangle
23) Distinct Subsequences Total
24) Maximum Subarray
25) Remove Duplicates from Sorted Array
26) Remove Duplicates from Sorted Array II
27) Longest Substring Without Repeating Characters
28) Longest Substring that contains 2 unique characters
29) Palindrome Partitioning
30) Reverse Words in a String
31) Find Minimum in Rotated Sorted Array
32) Find Peak Element
33) Min Stack
34) Majority Element
35) Combination Sum (DFS)
36) Best Time to Buy and Sell Stock
36) Best Time to Buy and Sell Stock II
36) Best Time to Buy and Sell Stock III (DP)

2. 链表

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

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


链表两个著名的应用是栈Stack和队列Queue。在Java标准库都都有实现,一个是Stack,另一个是LinkedList(Queue是它实现的接口)。

经典题目:
1) Add Two Numbers
2) Reorder List
3) Linked List Cycle
4) Copy List with Random Pointer
5) Merge Two Sorted Lists
6) Merge k Sorted Lists *
7) Remove Duplicates from Sorted List
8) Partition List

3. 树

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

  1. class TreeNode{
  2.         int value;
  3.         TreeNode left;
  4.         TreeNode right;
  5. }
复制代码


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

经典题目:
1) Binary Tree Preorder Traversal
2) Binary Tree Inorder Traversal
3) Binary Tree Postorder Traversal
4) Word Ladder
5) Validate Binary Search Tree
6) Flatten Binary Tree to Linked List
7) Path Sum
8) Construct Binary Tree from Inorder and Postorder Traversal
9) Convert Sorted Array to Binary Search Tree
10) Convert Sorted List to Binary Search Tree
11) Minimum Depth of Binary Tree
12) Binary Tree Maximum Path Sum *
13) Balanced Binary Tree

4. 图

图相关的问题主要集中在深度优先搜索(depth first search)和广度优先搜索(breath first search)。深度优先搜索很简单,广度优先要注意使用queue. 下面是一个简单的用队列Queue实现广度优先搜索。
  1. public class GraphTest {
  2.         public static void breathFirstSearch(GraphNode root, int x){
  3.                 if(root.val == x)
  4.                         System.out.println("find in root");
  5.                 Queue queue = new Queue();
  6.                 root.visited = true;
  7.                 queue.enqueue(root);
  8.                 while(queue.first != null){
  9.                         GraphNode c = (GraphNode) queue.dequeue();
  10.                         for(GraphNode n: c.neighbors){
  11.                                 if(!n.visited){
  12.                                         System.out.print(n + " ");
  13.                                         n.visited = true;
  14.                                         if(n.val == x)
  15.                                                 System.out.println("Find "+n);
  16.                                         queue.enqueue(n);
  17.                                 }
  18.                         }
  19.                 }
  20.         }
  21. }
复制代码


经典题目:
复制图(Clone Graph)

5. 排序

下面是不同排序算法的时间复杂度,你可以去wiki看一下这些算法的基本思想。
AlgorithmAverage TimeWorst TimeSpace
冒泡排序(Bubble sort)n^2n^21
选择排序(Selection sort)n^2n^21
插入排序(Insertion sort)n^2n^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;

  1. public static int f(int n){
  2.         if(n <= 2) return n;
  3.         int x = f(n-1) + f(n-2);
  4.         return x;
  5. }
复制代码


递归方法的时间复杂度是指数级,因为有很多冗余的计算:
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.         if (n <= 2){
  3.                 return n;
  4.         }
  5.         int first = 1, second = 2;
  6.         int third = 0;
  7.         for (int i = 3; i <= n; i++) {
  8.                 third = first + second;
  9.                 first = second;
  10.                 second = third;
  11.         }
  12.         return third;
  13. }
复制代码


这个例子迭代花费的时间更少,你可能复习一个两者的区别Recursion vs Iteration

7. 动态规划

动态规划是解决下面这些性质类问题的技术:
  • 一个问题可以通过更小子问题的解决方法来解决,或者说问题的最优解包含了其子问题的最优解
  • 有些子问题的解可能需要计算多次
  • 子问题的解存储在一张表格里,这样每个子问题只用计算一次
  • 需要额外的空间以节省时间
爬台阶问题完全符合上面的四条性质,因此可以用动态规划法来解决。
  1. public static int[] A = new int[100];
  2. public static int f3(int n) {
  3.         if (n <= 2)
  4.                 A[n]= n;
  5.         if(A[n] > 0)
  6.                 return A[n];
  7.         else
  8.                 A[n] = f3(n-1) + f3(n-2);//store results so only calculate once!
  9.         return A[n];
  10. }
复制代码


经典题目:
1) Edit Distance
2) Longest Palindromic Substring
3) Word Break
4) Maximum Subarray
5) Palindrome Partitioning

8. 位操作

常用位操作符:
OR (|)AND (&)XOR (^)Left Shift (<<)Right Shift (>>)Not (~)
1|0=11&0=01^0=10010<<2=10001100>>2=0011~1=0

用一个题目来理解这些操作 -
获得给定数字n的第i位:(i从0计数并从右边开始)
  1. public static boolean getBit(int num, int i){
  2.         int result = num & (1<<i);
  3.         if(result == 0){
  4.                 return false;
  5.         }else{
  6.                 return true;
  7. }
复制代码

例如,获得数字10的第2位:i=1, n=101<<1= 101010&10=1010 is not 0, so return true;


9. 概率问题

解决概率相关的问题通常需要先分析问题,下面是一个这类问题的简单例子:
一个房间里有50个人,那么至少有两个人生日相同的概率是多少?(忽略闰年的事实,也就是一年365天)
计算某些事情的概率很多时候都可以转换成先计算其相对面。在这个例子里,我们可以计算所有人生日都互不相同的概率,也就是:365/365 * 364/365 * 363/365 * … * (365-49)/365,这样至少两个人生日相同的概率就是1 – 这个值。
  1. public static double caculateProbability(int n){
  2.         double x = 1;
  3.         for(int i=0; i<n; i++){
  4.                 x *=  (365.0-i)/365.0;
  5.         }
  6.         double pro = Math.round((1-x) * 100);
  7.         return pro/100;
  8. }
  9. calculateProbability(50) = 0.97
复制代码


经典题目:桶中取球

10. 排列组合

组合和排列的区别在于次序是否关键。例1:
1、2、3、4、5这5个数字,用java写一个方法,打印出所有不同的排列, 如:51234、41235等。要求:"4"不能在第三位,"3"与"5"不能相连。
例2:
5个香蕉,4个梨子,3个苹果。同一种水果都是一样的,这个些水果排列成不同的组合有多少情况?

11. 其他类型的题目

主要是不能归到上面10大类的。需要寻找规律,然后解决问题的。 经典题目: Reverse Integer






已有(3)人评论

跳转到指定楼层
hb1984 发表于 2015-2-28 20:52:34
原来是刷leetcode啊。
回复

使用道具 举报

wangarlove 发表于 2015-3-4 13:52:47
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条