摘要:
O(N)扫一遍array就可以了: 找三个最大的, 两个最小的,然后根据第三个最大的正负来判断。 Approach 4: O(n) Time, O(1) Space Note – Step 1 and Step 2 can be done in single traversal of the arr 阅读全文
摘要:
是不是dp[i][j]是到a[i]和b[j]的最小diff之和 那么dp[i][j] = min( dp[i-1][j-1]+math.abs(a[i]-b[j]), dp[i][j-1]) 第一个是肯定包含b[j]的,第二个是肯定不包含b[j]的, 阅读全文
摘要:
recursive (O(logn) space) and iterative solutions (O(1) space) This is not a very intuitive problem for me, I have to spent quite a while drawing figu 阅读全文
摘要:
dp就是dp[1] = 1, dp[n] = dp[n - 1] + n, 公式应该是 n(n + 1) / 2 + 1 阅读全文
摘要:
Here is a way you can do it by hand. Basically the idea is we take in some numbers we don't want to generate, then we generate a random number and if 阅读全文
摘要:
break the loop at the last node which pointed to the entry. Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove 阅读全文
摘要:
第二题: 相当于实现Microsoft里面的查找替换的功能,给一个string article, 一个string find,一个string replace,把文章里所有的find都替换成replace,例如abcdbc把bc换成e >aede。思路比较简单,直接indexOf做了,只不过本来应该 阅读全文
摘要:
注意参数给的是List ,不知道实现的话get方法可能是o(n)的。一开始写了个用get的,doubt 我的时间复杂度,我说这个可能是o(n),那可能需要用iterator。考官说对那就写吧,然后秒写,这里楼主就突然傻逼了。这题相当于peeking iterator,双指针(双iterator)遍历 阅读全文
摘要:
方法2:进一步的方法是用HashSet, 每次取长度为10的字符串,O(N)时间遍历数组,重复就加入result,但这样需要O(N)的space, 准确说来O(N*10bytes), java而言一个char是2 bytes,所以O(N*20bytes)。String一大就MLE 最优解:是在方法2 阅读全文
摘要:
题目链接: https://leetcode.com/problems/nested-list-weight-sum/ Given a nested list of integers, return the sum of all integers in the list weighted by th 阅读全文