随笔分类 - 算法
摘要:堆有两种: max-heap 和 min-heap. Max-heap 一般用来排序,Min-heap 用来实现 priority queue.max-heap的定义是:for each i: A[parent(i)] >= A[i]min-heap: for each i: A[parent(i)...
阅读全文
摘要:Problem: Implement a function to check if a singly linked list is a palindrome.思路:最简单的方法是 Reverse and compare.另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形...
阅读全文
摘要:去Twitter面试的被问到这个问题,当时只想到了用HashMap的办法,这种办法时间复杂度O(n),空间复杂度是O(n), 更好的办法是用 FastRunner / SlowRunner approach。用两个pointer遍历链表,fast的速度是slow的两倍,如果有loop,二者一定会co...
阅读全文
摘要:Partition an array of integers around a value such taht all elements less than x come before elements greater than or equal to x.Idea: Just use of sub...
阅读全文
摘要:We all know how to search through an array for an element whose value equals the target value, but how to search for the element that has value greate...
阅读全文
摘要:Given a sorted array that has been rotated serveral times. Write code to find an element in this array. You may assume that the array was originally s...
阅读全文
摘要:Give a dynamic-programming solution to the 0-1 knapsack problem that runs inO(nW) time, where n is the number of items and W is the maximum weight ofi...
阅读全文
摘要:给一个长度为 n 的杆子,切成小段卖出去,价格根据小段的长度不同而不同。下面是一个例子我们要通过切成小段卖出尽可能高的总价钱。问题是:How to decompose the problem?Decomposition 的第一步是:第一刀切在哪?可以切在最左边(等于整根卖出去);可以切在位置1,位置...
阅读全文
摘要:所有的 DP 问题都可以简单得用 Recursion 的方式实现。这通常是最容易想到的思路。问题是这种实现不够 efficient,存在 subproblem 被重复计算的情况。有两种解决这个问题的方法:1. 很直观的,在 naive recursion 里加入 一个 save 的环境,把每个 su...
阅读全文
摘要:其一,所有的递归实现都可以 iteratively 实现,虽然很多时候递归的代码更简洁。其二,递归会产生多余的开销,包括空间和时间。其三,如果一定要递归,不要在一个递归函数里做多件事,最好只做一件。像下面的代码(未完成,因为写不下去了),试图在递归函数里做三件事,(1) 检测路径是否存在(2) 构造...
阅读全文
摘要:问题:给一个二叉树,写一个算法判断这个树是不是balanced。Solution #1.第一次遇到这个问题时我的解法,如下:public class Solution { public boolean isBalanced(TreeNode root) { if(root == ...
阅读全文
摘要:This algorithm deals with the general case, where G is a directed, weight graph, and it can contains negative edge weigths, cycles and even negative-w...
阅读全文
摘要:描述给你一个m x n (1 <= m, n <= 100)的矩阵A (0<=aij<=10000),要求在矩阵中选择一些数,要求每一行,每一列都至少选到了一个数,使得选出的数的和尽量的小。思路:这个题用DP做不了,因为Solution的结构不具备Optimal Substructure, 比如1 100 1001 100 100100 1 1Solution是1+1+1+1 = 4,但是该矩阵的子矩阵100 1001 1的Solution是 100+1 = 101. 看来只能用DFS做了。
阅读全文
摘要:We know that the longest path problem for general case belongs to the NP-hard category, so there is no known polynomial time solution for it. However, for a special case which is directed acyclic graph, we can solve this problem in linear time.First, take a look at an algorithm that sloves the short
阅读全文
摘要:We all know that the shortest path problem has optimal substructure. The reasoning is like below:Supppose we have a path p from node u to v, another node t lies on path p: u->t->v ("->" means a path).We claim that u->t is also a shortest path from u to t, and t->v is a short
阅读全文
摘要:Problem:此题的做法和 “检查单链表回文” 那道题的骨架是一样的,用 Recursive 的方法,简洁而优雅。参考回文那道题:http://www.cnblogs.com/Antech/p/3847752.htmlCode in Java:public class Solution { ...
阅读全文
摘要:This is a simple but important algorithm when dealing with singly linked list.This algorithms can reversely access a singly linked list using O(n) time. Below is my code in Java.class Node{ Node next; int val;}class SinglyLinkedList{ Node head; Node tail; //append a new Node t...
阅读全文
摘要:Binary Tree的三种Traversal方法老是记混,故于此总结下。Preorder: Node -> Left subtree -> Right subtreeInorder: Left subtree -> Node -> Right subtreePostorder: Left subtree -> Right subtree -> NodePre, In, Post指的是Node本身在[Node, Left, Right]中被访问的顺序。Pre is 1st, In is 2nd, Post is 3rd.Note: Inorder trave
阅读全文
摘要:Fudannlp:开源中文自然语言处理工具包|中文分词|词性标注|依存句法分析|指代消解。支持JAVA调用,和WebServices调用。ictclas:中科院研发的一个分词系统,支持的编程语言较丰富。
阅读全文

浙公网安备 33010602011771号