摘要: 一个数据序列的主元素,是指序列中出现次数超过序列长度一半的元素。法1(期望时间复杂度为O(n)):由于主元素出现次数超过序列长度的一半,因此,主元素一定是中位数。可以利用递归划分求中位数的方法,期望时间复杂度为O(n)。法2:显然,如果一个序列存在主元素,那么我们去掉序列中不同的两个数,剩下序列的主... 阅读全文
posted @ 2015-07-28 20:46 __brthls 阅读(588) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 /** 4 * @param nums: A list of integers. 5 * @return: An integer denotes the middle number of the array.... 阅读全文
posted @ 2015-07-28 20:01 __brthls 阅读(3938) 评论(0) 推荐(1) 编辑
摘要: http://www.lintcode.com/zh-cn/problem/unique-paths-ii/在上一题的基础上加入了障碍物。同样可采用递归实现,递推公式不变,但是需要加入对障碍物的判断。下面是实现的代码: 1 #include 2 #include 3 class Solution... 阅读全文
posted @ 2015-07-16 16:41 __brthls 阅读(276) 评论(0) 推荐(0) 编辑
摘要: http://www.lintcode.com/zh-cn/problem/unique-paths/递推公式:f[m][n] = f[m-1][n]+f[m][n-1]可采用DP或者记忆化的递归实现。下面是递归实现的代码: 1 #include 2 class Solution { 3 publ... 阅读全文
posted @ 2015-07-16 16:37 __brthls 阅读(159) 评论(0) 推荐(0) 编辑
摘要: 法1:二分 1 class Solution { 2 public: 3 /** 4 * @param x: An integer 5 * @return: The sqrt of x 6 */ 7 int sqrt(int x) { 8 ... 阅读全文
posted @ 2015-07-16 16:34 __brthls 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 1 #include 2 using std::vector; 3 4 template 5 void swap(vector &a, int i, int j) 6 { 7 T tmp = a[i]; 8 a[i] = a[j]; 9 a[j] = ... 阅读全文
posted @ 2015-07-15 16:57 __brthls 阅读(235) 评论(0) 推荐(0) 编辑
摘要: 1 #include 2 using std::vector; 3 4 template 5 class PriorityQueue 6 { 7 public: 8 PriorityQueue(int size = 101):bHeap(size),currentSiz... 阅读全文
posted @ 2015-07-15 14:03 __brthls 阅读(455) 评论(0) 推荐(0) 编辑
摘要: 1 #include 2 #include 3 #include 4 #include 5 using std::vector; 6 using std::list; 7 using std::string; 8 using std::find; 9 10 int hash(const s... 阅读全文
posted @ 2015-07-15 11:29 __brthls 阅读(245) 评论(0) 推荐(0) 编辑
摘要: 1 inline int max(int x, int y){ return x>y?x:y; } 2 3 template 4 class AVLTree 5 { 6 public: 7 AVLTree():root(NULL) {} 8 ... 阅读全文
posted @ 2015-07-14 11:24 __brthls 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 1 template 2 class BinarySearchTree 3 { 4 public: 5 BinarySearchTree():root(NULL){} 6 BinarySearchTree(const BinarySearchTr... 阅读全文
posted @ 2015-07-12 19:50 __brthls 阅读(331) 评论(0) 推荐(0) 编辑