摘要: 花式反转单链表 1. 用反转数组的思路:反转[1:] 然后把head 怼在后面。与数组不同“怼在后面” 这个操作是O(n) 效率很差。 2. 改进上面的方法,优化掉后面那个O(n) 的添加操作。方法是:记住已反转好的tail,这样只用tail->next = head 就可以了,不需要遍历整个链表。 阅读全文
posted @ 2019-08-14 15:43 Agentgamer 阅读(133) 评论(0) 推荐(0) 编辑
摘要: class Solution { unordered_set visited; inline string hash(int r, int c) { return to_string(r) + "#" + to_string(c); } void check(vector>& board, ... 阅读全文
posted @ 2019-07-19 20:39 Agentgamer 阅读(180) 评论(0) 推荐(0) 编辑
摘要: https://leetcode.com/problems/bag-of-tokens/ 一开始觉得应该是个dp 题,把所有结果搜出来然后max 一下。实现以后发现组合太多了,非常慢,即使加上memorization 也是TLE 看了答案发现是用greedy,然而也没有证明为啥greedy 就是最优 阅读全文
posted @ 2019-03-26 22:53 Agentgamer 阅读(200) 评论(0) 推荐(0) 编辑
摘要: https://www.lintcode.com/problem/ugly-number-ii/description 2 * (1,2,3,4,5....) 3 * (1,2,3,4,5....) 5 * (1,2,3,4,5....) 三个stream 的merge 阅读全文
posted @ 2019-03-10 22:47 Agentgamer 阅读(142) 评论(0) 推荐(0) 编辑
摘要: https://leetcode.com/problems/distribute-candies/description/ 题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一份里面数字的多样性最大。 思路:一个很简单的思路是,数出有多少不一样的数字n,然后一份有多少个m,取 阅读全文
posted @ 2018-10-15 11:04 Agentgamer 阅读(85) 评论(0) 推荐(0) 编辑
摘要: https://leetcode.com/problems/pascals-triangle/description/ 思路:先写好base case,也就是杨辉三角的前两行。然后从第2 行开始递推第3 行。往后的每一行都由前一行推出。 知道第n 行求n+1 行很简单,首先开头都是1,然后从0 开始 阅读全文
posted @ 2018-10-15 10:14 Agentgamer 阅读(115) 评论(0) 推荐(0) 编辑
摘要: https://leetcode.com/problems/subtree-of-another-tree/description/ easy 题也不easy,坑还是多。主要观察两个例子:[1,1] [1] 和 [3,4,5,1,null,null,2] [3,1,2] 第一个例子主要考察当s, t 阅读全文
posted @ 2018-10-12 17:05 Agentgamer 阅读(109) 评论(0) 推荐(0) 编辑
摘要: https://leetcode.com/problems/longest-valid-parentheses/description/ 来回做了好几遍,虽然AC 了但是这个解发肯定不是最优的。思路是把问题分两步解决:1. 把字符串转换成一个匹配括号的id 序列,比如第0 个和第3 个匹配了,则把0 阅读全文
posted @ 2018-10-12 15:12 Agentgamer 阅读(122) 评论(0) 推荐(0) 编辑
摘要: https://leetcode.com/problems/reshape-the-matrix/description/ 简单有趣,也稍微窥探了一下matlab 和numpy 里面reshape 的算法。当然现实要比这个题要复杂,比如numpy 里面reshape 可以只接受一个参数,然后自动推导 阅读全文
posted @ 2018-10-11 12:36 Agentgamer 阅读(160) 评论(0) 推荐(0) 编辑
摘要: https://leetcode.com/problems/binary-tree-tilt/description/ 挺好的一个题目,审题不清的话很容易做错。主要是tilt of whole tree 的定义是sum of all node's tilt 而不是想当然的tilt of root. 阅读全文
posted @ 2018-10-11 11:59 Agentgamer 阅读(133) 评论(0) 推荐(0) 编辑