摘要: 优先队列的实现 优先队列是拥有权值观念的队列,它允许加入新元素,移除旧元素、审视元素值等功能。由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素,除此之外无其他存取元素的途径。 优先队列带有权值观念,其内的元素并非按照被推入的次序排列,而是自动依照元素的权值排列(通常权值以实值表... 阅读全文
posted @ 2015-01-02 18:33 vincently 阅读(345) 评论(0) 推荐(0) 编辑
摘要: 栈的实现(数组) 1 template class ArrayStack { 2 public: 3 ArrayStack(int c = 100): capacity(c), top(-1) { 4 data = new T[capacity](); 5 } 6 ... 阅读全文
posted @ 2014-12-31 17:03 vincently 阅读(365) 评论(0) 推荐(0) 编辑
摘要: 1. 堆 堆是一颗被完全填满的二叉树,可能的例外是在底层,底层上的元素从左到右填入。这样的树称为完全二叉树。对于一个n个节点的完全二叉树,树高为logN.一个重要的观察发现,因为完全二叉树很有规律,所以可以用一个数组表示而不需要使用链。这种用数组表示树的方法称为隐式表述法(implicit rep... 阅读全文
posted @ 2014-12-30 13:42 vincently 阅读(604) 评论(0) 推荐(0) 编辑
摘要: 在非负整数集上定义一个函数f,它满足f(0)=0,且f(x)=2f(x-1)+x^2.从这个定义可以看出f(1)=1,f(2)=6,f(3)=21,f(4)=58。当一个函数用自身定义时就称为递归(recursive).即,一个函数直接或间接地调用自身,是为直接或间接递归。C++是允许递归的。但... 阅读全文
posted @ 2014-12-29 15:19 vincently 阅读(4482) 评论(0) 推荐(0) 编辑
摘要: Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,... 阅读全文
posted @ 2014-12-01 17:11 vincently 阅读(221) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum =... 阅读全文
posted @ 2014-11-29 11:45 vincently 阅读(188) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Fo... 阅读全文
posted @ 2014-11-29 10:53 vincently 阅读(170) 评论(0) 推荐(0) 编辑
摘要: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.注意写出鲁棒性的代... 阅读全文
posted @ 2014-11-27 22:09 vincently 阅读(111) 评论(0) 推荐(0) 编辑
摘要: Implement pow(x,n).思路:像string to integer一样。考虑的细节较多。 1.测试用例要考虑基数和指数都为正数、负数和零的情况。 2.注意0的0次方在数学上没有意义。 3.考虑底数为0且指数为负数的处理方法。可以有返回值、全局变量和异常。这里使用全局变量区别... 阅读全文
posted @ 2014-11-26 21:39 vincently 阅读(316) 评论(0) 推荐(0) 编辑
摘要: You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb... 阅读全文
posted @ 2014-11-26 11:19 vincently 阅读(159) 评论(0) 推荐(0) 编辑