随笔分类 - LintCode
摘要:一个数据序列的主元素,是指序列中出现次数超过序列长度一半的元素。法1(期望时间复杂度为O(n)):由于主元素出现次数超过序列长度的一半,因此,主元素一定是中位数。可以利用递归划分求中位数的方法,期望时间复杂度为O(n)。法2:显然,如果一个序列存在主元素,那么我们去掉序列中不同的两个数,剩下序列的主...
阅读全文
摘要:1 class Solution { 2 public: 3 /** 4 * @param nums: A list of integers. 5 * @return: An integer denotes the middle number of the array....
阅读全文
摘要:http://www.lintcode.com/zh-cn/problem/unique-paths-ii/在上一题的基础上加入了障碍物。同样可采用递归实现,递推公式不变,但是需要加入对障碍物的判断。下面是实现的代码: 1 #include 2 #include 3 class Solution...
阅读全文
摘要: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...
阅读全文
摘要:法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 ...
阅读全文

浙公网安备 33010602011771号