Category Archives: 算法

Leetcode 295. Find Median from Data Stream

题目链接295. Find Median from Data Stream

  在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
(more…)

最小二乘法的极大似然解释

  最开始学习机器学习的时候,首先遇到的就是回归算法,回归算法里最最重要的就是最小二乘法,为什么损失函数要用平方和,而且还得是最小?仔细想想最小二乘法视乎很合理,但是合理在哪,怎么用数学方法来证明它合理。
(more…)

Leetcode 19.Remove Nth Node From End of List

题目链接:19. Remove Nth Node From End of List

  删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
(more…)

Leetcode 114. Flatten Binary Tree to Linked List

Leetcode 114. Flatten Binary Tree to Linked List

  题目意思很简单,就是把一棵二叉数转换为链表,虽然题目中没说以什么样的形式转换,但看下样例就很容易看出来,是以先序遍历的次序转换成链表。这里链表节点还是treenode,只不过它的左节点为空而已。
(more…)

Leetcode 467. Unique Substrings in Wraparound String

题目链接:Unique Substrings in Wraparound String
这里加段英文,不是为了凑字数,而是为了让别人搜索题目的时候能搜到我的博客。。
Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s will look like this: “…zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd….”.
(more…)

Leetcode 368. Largest Divisible Subset

题目链接:368. Largest Divisible Subset

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.
If there are multiple solutions, return any subset is fine.
  题目意思也很简单,给出一个不含重复数字的数组,找到最长的一个子数组,子数组里的元素必须两两整除。
(more…)

求无序数组排序后相邻俩数最大差值(思路及详解)

  前两天在一个学长面试的时候遇到这样一个题,这里稍微详细说下本文的标题。给你n个任意整数,求排序后相邻两个数之间的最大差值,这里n可能有10^5,整数为任意32位整型。要求求解算法的时间复杂度为O(n)。

(more…)

Leetcode 240. Search a 2D Matrix II

题目链接:Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.

  此题是74题Search a 2D Matrix的升级版,所给出的矩阵性质相对74题少了一条,只保证了每行和每列都是增序的,但依旧有O(m+n)的解法。

(more…)

Leetcode 74. Search a 2D Matrix

题目链接:Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
* Integers in each row are sorted from left to right.
* The first integer of each row is greater than the last integer of the previous row.

  这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。

(more…)

Leetcode 313. Super Ugly Number

Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.

原题链接:Super Ugly Number

(more…)

Leetcode 11. Container With Most Water

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.

原题链接:Container With Most Water

(more…)

Leetcode 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

原题链接:Longest Substring Without Repeating Characters

  此题题意是找出一个不包含相同字母的最长子串,题目给出了两个例子来说明题意,但这两个例子具有误导性,让你误以为字符串中只有小写字母。还好我是心机boy,我把大写字母的情况也给考虑进去了,不过。。。。字符串里竟然有特殊字符,于是贡献了一次wrong answer,这次我把ascii字符表都考虑进去,然后就没问题了。这个故事告诫我们在编程处理问题的时候一定要注意输入数据的范围,面试中可以和面试官去确认数据范围,也能显得你比较严谨。

(more…)

Leetcode 198. House Robber

原题链接:198. House Robber

  一句话理解题意,有个偷马贼晚上要偷尽可能值钱的马,但连续两头马被偷会触发报警,问他如何在不触发报警(不偷连续的两匹马)的情况下偷到总价值最高马,返回最高总价值。
  看到maximum,就应该想到这是应该求解最优的问题,一想到求解最优,一般除了暴力就是动态规划了。

(more…)