马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下是在编程面试中排名前10的算法相关的概念,我会通过一些简单的例子来阐述这些概念。由于完全掌握这些概念需要更多的努力,因此这份列表只是作为一个介绍。本文将从Java的角度看问题,包含下面的这些概念: 1. 字符串
2. 链表
3. 树
4. 图
5. 排序
6. 递归 vs. 迭代
7. 动态规划
8. 位操作
9. 概率问题
10. 排列组合 1. 字符串 如果IDE没有代码自动补全功能,所以你应该记住下面的这些方法。 [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
| [size=1em][size=1em]toCharArray() // 获得字符串对应的char数组
[size=1em]Arrays.sort() // 数组排序
[size=1em]Arrays.toString(char[] a) // 数组转成字符串
[size=1em]charAt(int x) // 获得某个索引处的字符
[size=1em]length() // 字符串长度
[size=1em]length // 数组大小
|
2. 链表 在Java中,链表的实现非常简单,每个节点Node都有一个值val和指向下个节点的链接next。 [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
| [size=1em][size=1em]class Node {
[size=1em] int val;
[size=1em] Node next;
[size=1em] Node(int x) {
[size=1em] val = x;
[size=1em] next = null;
[size=1em] }
[size=1em]}
|
链表两个著名的应用是栈Stack和队列Queue。 栈: [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
| [size=1em][size=1em]class Stack{
[size=1em] Node top;
[size=1em] public Node peek(){
[size=1em] if(top != null){
[size=1em] return top;
[size=1em] }
[size=1em] return null;
[size=1em] }
[size=1em] public Node pop(){
[size=1em] if(top == null){
[size=1em] return null;
[size=1em] }else{
[size=1em] Node temp = new Node(top.val);
[size=1em] top = top.next;
[size=1em] return temp;
[size=1em] }
[size=1em] }
[size=1em] public void push(Node n){
[size=1em] if(n != null){
[size=1em] n.next = top;
[size=1em] top = n;
[size=1em] }
[size=1em] }
[size=1em]}
|
队列: [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
| [size=1em][size=1em]class Queue{
[size=1em] Node first, last;
[size=1em] public void enqueue(Node n){
[size=1em] if(first == null){
[size=1em] first = n;
[size=1em] last = first;
[size=1em] }else{
[size=1em] last.next = n;
[size=1em] last = n;
[size=1em] }
[size=1em] }
[size=1em] public Node dequeue(){
[size=1em] if(first == null){
[size=1em] return null;
[size=1em] }else{
[size=1em] Node temp = new Node(first.val);
[size=1em] first = first.next;
[size=1em] return temp;
[size=1em] }
[size=1em] }
[size=1em]}
|
3. 树 这里的树通常是指二叉树,每个节点都包含一个左孩子节点和右孩子节点,像下面这样: [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
| [size=1em][size=1em]class TreeNode{
[size=1em] int value;
[size=1em] TreeNode left;
[size=1em] TreeNode right;
[size=1em]}
|
下面是与树相关的一些概念: - 平衡 vs. 非平衡:平衡二叉树中,每个节点的左右子树的深度相差至多为1(1或0)。
- 满二叉树(Full Binary Tree):除叶子节点以为的每个节点都有两个孩子。
- 完美二叉树(Perfect Binary Tree):是具有下列性质的满二叉树:所有的叶子节点都有相同的深度或处在同一层次,且每个父节点都必须有两个孩子。
- 完全二叉树(Complete Binary Tree):二叉树中,可能除了最后一个,每一层都被完全填满,且所有节点都必须尽可能想左靠。
译者注:完美二叉树也隐约称为完全二叉树。完美二叉树的一个例子是一个人在给定深度的祖先图,因为每个人都一定有两个生父母。完全二叉树可以看成是可以有若干额外向左靠的叶子节点的完美二叉树。疑问:完美二叉树和满二叉树的区别?(参考:http://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html) 4. 图 图相关的问题主要集中在深度优先搜索(depth first search)和广度优先搜索(breath first search)。 下面是一个简单的图广度优先搜索的实现。 1) 定义GraphNode [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
| [size=1em][size=1em]class GraphNode{
[size=1em] int val;
[size=1em] GraphNode next;
[size=1em] GraphNode[] neighbors;
[size=1em] boolean visited;
[size=1em] GraphNode(int x) {
[size=1em] val = x;
[size=1em] }
[size=1em] GraphNode(int x, GraphNode[] n){
[size=1em] val = x;
[size=1em] neighbors = n;
[size=1em] }
[size=1em] public String toString(){
[size=1em] return "value: "+ this.val;
[size=1em] }
[size=1em]}
|
2) 定义一个队列Queue [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
| [size=1em][size=1em]class Queue{
[size=1em] GraphNode first, last;
[size=1em] public void enqueue(GraphNode n){
[size=1em] if(first == null){
[size=1em] first = n;
[size=1em] last = first;
[size=1em] }else{
[size=1em] last.next = n;
[size=1em] last = n;
[size=1em] }
[size=1em] }
[size=1em] public GraphNode dequeue(){
[size=1em] if(first == null){
[size=1em] return null;
[size=1em] }else{
[size=1em] GraphNode temp = new GraphNode(first.val, first.neighbors);
[size=1em] first = first.next;
[size=1em] return temp;
[size=1em] }
[size=1em] }
[size=1em]}
|
3) 用队列Queue实现广度优先搜索 [backcolor=white !important][size=1em][size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
[size=1em]29
[size=1em]30
[size=1em]31
[size=1em]32
[size=1em]33
[size=1em]34
[size=1em]35
[size=1em]36
[size=1em]37
[size=1em]38
[size=1em]39
[size=1em]40
[size=1em]41
| [size=1em][size=1em]public class GraphTest {
[size=1em] public static void main(String[] args) {
[size=1em] GraphNode n1 = new GraphNode(1);
[size=1em] GraphNode n2 = new GraphNode(2);
[size=1em] GraphNode n3 = new GraphNode(3);
[size=1em] GraphNode n4 = new GraphNode(4);
[size=1em] GraphNode n5 = new GraphNode(5);
[size=1em] n1.neighbors = new GraphNode[]{n2,n3,n5};
[size=1em] n2.neighbors = new GraphNode[]{n1,n4};
[size=1em] n3.neighbors = new GraphNode[]{n1,n4,n5};
[size=1em] n4.neighbors = new GraphNode[]{n2,n3,n5};
[size=1em] n5.neighbors = new GraphNode[]{n1,n3,n4};
[size=1em] breathFirstSearch(n1, 5);
[size=1em] }
[size=1em] public static void breathFirstSearch(GraphNode root, int x){
[size=1em] if(root.val == x)
[size=1em] System.out.println("find in root");
[size=1em] Queue queue = new Queue();
[size=1em] root.visited = true;
[size=1em] queue.enqueue(root);
[size=1em] while(queue.first != null){
[size=1em] GraphNode c = (GraphNode) queue.dequeue();
[size=1em] for(GraphNode n: c.neighbors){
[size=1em] if(!n.visited){
[size=1em] System.out.print(n + " ");
[size=1em] n.visited = true;
[size=1em] if(n.val == x)
[size=1em] System.out.println("Find "+n);
[size=1em] queue.enqueue(n);
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em]}
|
Output:
|