# 面试10大算法汇总＋常见题目解答

1. 字符串和数组

```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```

2. 链表

```class Node { int val; Node next;   Node(int x) { val = x; next = null; } }```

3. 树

```class TreeNode{ int value; TreeNode left; TreeNode right; }```

2) Binary Tree Inorder Traversal [Palantir]
3) Binary Tree Postorder Traversal
4) Binary Tree Level Order Traversal
4) Binary Tree Level Order Traversal II
5) Validate Binary Search Tree
6) Flatten Binary Tree to Linked List
7) Path Sum (DFS or BFS)
7) Path Sum II (DFS)
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
14) Symmetric Tree
15) Binary Search Tree Iterator
16) Binary Tree Right Side View
17) Implement Trie (Prefix Tree)
18) Add and Search Word - Data structure design (DFS)

4. 图

```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. 排序

 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

6. 递归 vs. 迭代

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; }```

7. 动态规划

1. 一个问题可以通过更小子问题的解决方法来解决，或者说问题的最优解包含了其子问题的最优解
2. 有些子问题的解可能需要计算多次
3. 子问题的解存储在一张表格里，这样每个子问题只用计算一次
4. 需要额外的空间以节省时间

```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

```public static boolean getBit(int num, int i){ int result = num & (1<<i);   if(result == 0){ return false; }else{ return true; }```

i=1, n=10
1<<1= 10 1010&10=10 10 is not 0, so return true;

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、2、3、4、5这5个数字，用java写一个方法，打印出所有不同的排列， 如：51234、41235等。要求："4"不能在第三位，"3"与"5"不能相连。

5个香蕉，4个梨子，3个苹果。同一种水果都是一样的，这个些水果排列成不同的组合有多少情况？

11. 其他类型的题目

Category >> Algorithms >> Interview >> Top 10
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
1. XtraLarge on 2014-12-23

Thanks to programcreek.com, now I’m not worried about I have no place to find valuable answers to common java problems.

2. Sissi Peng on 2015-9-18

This is a very good summary! Thank you!

3. 谢硕 on 2015-9-22