O(n) time O(h) space
单调递减栈,因为对于每一个数x,都要找从左开始第一个比它小的数y,x是y的右子
1 | /** |
recursive O(n) time O(h) space
1 | /** |
O(n) time O(h) space
单调递减栈,因为对于每一个数x,都要找从左开始第一个比它小的数y,x是y的右子
1 | /** |
recursive O(n) time O(h) space
1 | /** |
O(n) time O(h) space
1 | /** |
O(n) time O(h + to_delete) space
判断是否需要删除需要hashtable
如果需要删除必须『真』删除,采用重新赋左右子的值的方法来删除
如果当前root需要删除,则把左右子树遍历删除后的结果尝试加入森林,所以需要后序遍历
如果当前root不需要删除,则update左右子之后返回即可(注意root并不一定是森林里的一棵树的根)
1 | /** |
O(n)
1 | class Solution { |
复杂度不是O(n)
1 | class Solution { |
负号法 O(n) time O(1) space
1 | class Solution { |
O(n) time O(26) space
1 | class Solution { |
O(nlogx) x是最大的分母
1 | class Solution { |
trie的应用 构造函数 O(nm) n是单词个数 m是单词长度 查询函数 O(k) k是prefix+suffix长度 空间复杂度 O(nm^2) 因为每个单词都可以派生出m个单词 每个单词长度为m 共n个单词 就是nmm
这道题最tricky的地方是如何存单词
Consider the word ‘apple’. For each suffix of the word, we could insert that suffix, followed by ‘#’, followed by the word, all into the trie.
For example, we will insert ‘#apple’, ‘e#apple’, ‘le#apple’, ‘ple#apple’, ‘pple#apple’, ‘apple#apple’ into the trie. Then for a query like prefix = “ap”, suffix = “le”, we can find it by querying our trie for le#ap.
1 | class WordFilter { |
dfs O(n) time O(h) space
1 | /* |
iterative
1 | /* |
dfs O(n) time O(h) space
1 | /* |
iterative
1 | /* |