08 2019 档案

摘要:problem: https://leetcode.com/problems/robot-bounded-in-circle/ As the hint mentions, we can keep track of the robot's position, when it finally retur 阅读全文
posted @ 2019-08-18 20:02 fish1996 阅读(165) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/knight-probability-in-chessboard/ This is an easy problem using dfs. 阅读全文
posted @ 2019-08-17 23:58 fish1996 阅读(96) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/advantage-shuffle/ In ordered to maximize the advantage of array A with respect to B, we had better choose the 阅读全文
posted @ 2019-08-17 23:39 fish1996 阅读(131) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/three-equal-parts/ 首先,检测有多少个1,记作x,看是不是3的倍数。不是则说明不存在对应划分。 之后,检测末尾的0,作为每个二进制数末尾的0个数,记作y。 最后,检测是否存在3个连续、不相交的,总共包含 阅读全文
posted @ 2019-08-16 21:47 fish1996 阅读(216) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/network-delay-time 相当于求单源带权最短路径。然后取所有最短路径中最大的那个。 阅读全文
posted @ 2019-08-12 13:27 fish1996 阅读(195) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/longest-repeating-character-replacement/ 维护一个最多包含k个额外字符的滑动窗口。需要记录当前出现次数最多字符的出现次数来判断窗口是否合法,如果超过了,就把首指针向后挪一位,同时更新最 阅读全文
posted @ 2019-08-11 22:14 fish1996 阅读(114) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/minimum-window-substring/ 滑动窗口题。维护一个包含t中所有字符的最小滑动窗口,首先用一个hashmap记录所有t中的字符和出现次数,在s中每遇到一次计数器加一,找到了符合条件的窗口后,尝试向右移动窗 阅读全文
posted @ 2019-08-11 21:39 fish1996 阅读(141) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/permutation-sequence 求第k个排列数。数学解法,可以找一下规律。比如对于1234的排列数,一共有24种。我们从左到右依次决定排列数是哪些。首先第一个数有4种可选的,一共有24种,那么每种就是6个,我们用 阅读全文
posted @ 2019-08-11 21:18 fish1996 阅读(90) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/permutations/ 输出所有排列数。递归中,最快的方法是直接交换,实际上执行的是选择操作,选择了一个数据后,把它交换到前面;可以保证下一次选择不会包含着一数字,并且数字被选择的概率都是相等的。 阅读全文
posted @ 2019-08-11 18:26 fish1996 阅读(107) 评论(0) 推荐(0)
摘要:前序遍历: 中序遍历: 后序遍历: 阅读全文
posted @ 2019-08-11 17:53 fish1996 阅读(344) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/find-peak-element/ 根据下标二分搜索。每次检查mid左右两个数和mid的关系,决定如何进行下一步。 阅读全文
posted @ 2019-08-11 17:32 fish1996 阅读(156) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 经典二分搜索题。要点是改变low或high的时候把当前数字mid也包含进来,因为它也可能是结果。 阅读全文
posted @ 2019-08-11 16:38 fish1996 阅读(111) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/search-a-2d-matrix-ii 经典双指针二分查找题目。 阅读全文
posted @ 2019-08-11 16:20 fish1996 阅读(102) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/next-greater-node-in-linked-list/ 维护递减的单调栈。这道题对象是链表,不像数组可以快速通过下标索引,所以比较方便的做法是在栈中同时记录数字和对应的下标,并且默认填0,如果找到了比它大的第一个 阅读全文
posted @ 2019-08-11 16:05 fish1996 阅读(117) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/largest-rectangle-in-histogram 经典单调栈题目。维护递增的单调栈,当发现当前数字比栈顶要小的时候,此时栈顶元素是最大的(大于栈里的下一个元素,也大于当前元素),所以可以计算以当前栈顶元素为高的矩 阅读全文
posted @ 2019-08-11 15:12 fish1996 阅读(91) 评论(0) 推荐(0)
摘要:这一次终于没有提交WA了。 1154. Day of the Year 简单题。打印一年到现在的天数,区分闰年即可。// 不要问我run, ping是啥意思 = =。 1155. Number of Dice Rolls With Target Sum 中等题,动态规划。给你d个骰子,包含数字为1~ 阅读全文
posted @ 2019-08-11 13:46 fish1996 阅读(212) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/find-the-duplicate-number/ 查找是否存在环。快指针走2步,慢指针走1步,相遇则存在环。设慢指针走了路程s,则快指针走了2s,环长度为s。若环的起始位置为d,则慢指针的位置在起始位置过一点的地方,它只 阅读全文
posted @ 2019-08-11 02:48 fish1996 阅读(198) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/search-in-rotated-sorted-array/ 二分搜索题。检查二分后的半段是否满足递增条件,如果满足且数据落在这一区间,就在这一区间查找,否则到另一区间查找。 阅读全文
posted @ 2019-08-11 02:07 fish1996 阅读(125) 评论(0) 推荐(0)
摘要:最后一题比赛快结束的时候想到怎么做了(通过WA的数据猜出来的),比赛后10分钟做出来的。最终做了3题,时间1个小时左右吧。 1150. Check If a Number Is Majority Element in a Sorted Array 这道题理论应该用二分,但是数据量很小(1000),所 阅读全文
posted @ 2019-08-11 00:29 fish1996 阅读(280) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/search-a-2d-matrix/ 经典二分搜索。本质上还是一维数组。 阅读全文
posted @ 2019-08-10 22:04 fish1996 阅读(129) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/count-numbers-with-unique-digits/ 1 : 10 2: 10 + 9 x 9 3: 10 + 9 x 9 + 9 x 9 x 8 4: 10 + 9 x 9 + 9 x 9 x 8 x 7 … 阅读全文
posted @ 2019-08-10 19:12 fish1996 阅读(153) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/russian-doll-envelopes/ 最长连续子序列类型问题。先排序,dp[i]记录使用第i个套娃的最大数量。 阅读全文
posted @ 2019-08-10 18:19 fish1996 阅读(205) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/integer-break/ 枚举所有整数相加,取它们最大乘积的乘积,然后选择最大值。 阅读全文
posted @ 2019-08-10 18:06 fish1996 阅读(123) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/counting-bits 爬台阶类型。我的做法是num拆分为比它小的最大2^n的值加上另一个数,然后这两个的dp叠加。 阅读全文
posted @ 2019-08-10 17:31 fish1996 阅读(158) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/create-maximum-number/ 分治。计算第一个数组最大的和第二个数组最大的,然后结合起来找最大的。 阅读全文
posted @ 2019-08-10 17:21 fish1996 阅读(284) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/remove-k-digits 单调栈。维护一个递增的栈,每pop一次意味着移除了一个元素,k--。减为0时不再移除。前导0处理起来很麻烦,很容易WA。 阅读全文
posted @ 2019-08-10 16:18 fish1996 阅读(122) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/perfect-squares 数字类dp。查找当前数字减去一个平方数对应的最小拆分次数。 阅读全文
posted @ 2019-08-10 13:56 fish1996 阅读(91) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/ugly-number-ii 大的ugly number是由小的ugly number乘以系数2, 3, 5得到的,每次相乘后,取最小的作为下一个,然后把最小的值的当前指针向后挪一位。(用vector封装了一下速度更慢了, 阅读全文
posted @ 2019-08-10 13:34 fish1996 阅读(115) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/house-robber-ii/ 多状态转换dp。我的方法是维护了四个状态。用两趟dp的基本思想也是多个状态。 阅读全文
posted @ 2019-08-10 12:27 fish1996 阅读(158) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/dungeon-game 看了这道题的tag我用了二分 + 简化dp(只需求特定血量能否达到)来做,能过但是速度好慢。一看评论区发现大家都是用纯dp过的,我哭了。 阅读全文
posted @ 2019-08-10 11:47 fish1996 阅读(177) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/maximum-product-subarray 类似买卖股票,需要维护两个状态,当前最大数和最小数。 阅读全文
posted @ 2019-08-10 01:01 fish1996 阅读(94) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/word-break/ 划分类型问题。 阅读全文
posted @ 2019-08-10 00:16 fish1996 阅读(164) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/palindrome-partitioning-ii/ 爬台阶类型问题。先计算出所有可能的回文串,dp[ i ] 代表前 i 个字符的最小划分,找到以 i 结尾的所有回文串,取划分最小的那个作为结果。 阅读全文
posted @ 2019-08-09 23:54 fish1996 阅读(156) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/distinct-subsequences/ 字符匹配类型题目。 阅读全文
posted @ 2019-08-09 23:02 fish1996 阅读(126) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/wildcard-matching/ 用记忆化搜索做的,不容易出错: 非递归: 阅读全文
posted @ 2019-08-09 22:24 fish1996 阅读(157) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/unique-binary-search-trees/ 左子树个数 * 右子树个数 阅读全文
posted @ 2019-08-09 22:00 fish1996 阅读(127) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/unique-binary-search-trees-ii/ 用递归传最小值和最大值写起来代码应该会更简练,此处用的是递推写的。 阅读全文
posted @ 2019-08-09 21:49 fish1996 阅读(415) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/unique-paths-ii 爬台阶类型问题(2D), 如果当前位置有障碍物就不进行状态转换。 阅读全文
posted @ 2019-08-09 16:28 fish1996 阅读(157) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/longest-string-chain/ 爬台阶类型问题。从长度为k转换为长度为k + 1的字符串。此处通过删掉一个字符,查看删掉后的字符串是否存在来判断能否转换。 阅读全文
posted @ 2019-08-09 14:27 fish1996 阅读(393) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/partition-equal-subset-sum/ 经典背包问题。找到是否存在恰好装满sum / 2的物体,可以优化为1D的。 阅读全文
posted @ 2019-08-09 12:58 fish1996 阅读(153) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/largest-sum-of-averages 爬台阶类型dp。递推考虑的不只是从前i个数字到前i + 1个数字,还需要考虑从划分为k到划分为k + 1组,相当于在后面叠加一组。 记忆搜索做法: dp做法: 滚动数组优化空间 阅读全文
posted @ 2019-08-09 11:33 fish1996 阅读(193) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/longest-arithmetic-sequence/description/ 最长子序列类型问题。因为状态比较多,可以存在hash表里,之后直接查找。 阅读全文
posted @ 2019-08-09 00:53 fish1996 阅读(283) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/edit-distance/ 二维爬台阶。dp[ i ][ j ] 代表长度为i的word1和长度为j的word2的最小转换次数。dp[ i ][ j ]可以由dp[ i ][ j - 1] 或 dp[ i - 1][j - 阅读全文
posted @ 2019-08-08 21:52 fish1996 阅读(109) 评论(0) 推荐(0)
摘要:problem: 类背包。周赛题,比赛时用了记忆搜索,之后学习了一波常规dp做法: dp[ i ] 代表前i本书的最小高度,每加入一本新书后,我们需要试着把上一层的书放到下面来,看哪种情况高度最小,作为当前结果。 第二个循环逆序是因为我们希望利用上一个大循环里的结果,从高往低读可以避免结果被当前循环 阅读全文
posted @ 2019-08-08 21:14 fish1996 阅读(342) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/palindromic-substrings/ 对于每个字符,找到前面和它匹配的字符,再看中间夹着的字符串是不是len - 2的回文串,如果是,结果加一。 阅读全文
posted @ 2019-08-08 20:34 fish1996 阅读(128) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/2-keys-keyboard/ 重做此题,发现依然只能写出N2的算法,水平并没有提升TAT 从评论区拿到的advance版本。这道题的标准解法是分解质因数。最优解的证明已经涉及到一些数学上的问题了。 阅读全文
posted @ 2019-08-08 19:34 fish1996 阅读(204) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/scramble-string/ 记忆化搜索。在所有可能的交换情况中找到合适的。 阅读全文
posted @ 2019-08-08 17:57 fish1996 阅读(160) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/minimum-path-sum/ 爬台阶类型dp。从上方或左方取累加最小值。 阅读全文
posted @ 2019-08-08 16:50 fish1996 阅读(127) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/delete-and-earn/ 爬台阶类型dp。首先需要进行排序,并记录每个数出现的数字,为了方便这里使用了ordered_map。因为情况是对称的,可以只考虑前一位数字是否比当前位数字恰好小1, 如果是,就需要从dp[ 阅读全文
posted @ 2019-08-08 16:21 fish1996 阅读(218) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/video-stitching/ 背包类型问题。可将长度视为背包容量,物体视为长条状的物体。 阅读全文
posted @ 2019-08-08 15:25 fish1996 阅读(433) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 记忆化搜索。这道题主要的难点在于如何划分子问题,以某一点为基础,直接一分为二是会漏掉不少情况的;而由于边一定存在于某个三角形中,我们可以选定特定 阅读全文
posted @ 2019-08-08 14:13 fish1996 阅读(157) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/coin-change/ 类似背包的简单dp问题。 阅读全文
posted @ 2019-08-08 10:48 fish1996 阅读(98) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/unique-substrings-in-wraparound-string/ 这道题是最长子字符串类型的,虽然tag是动态规划,但实际上我用了hash来做。 阅读全文
posted @ 2019-08-08 10:21 fish1996 阅读(120) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/delete-columns-to-make-sorted-iii/ 这是一道伪装成2维的1维最长子序列类型的dp题目,虽然打着hard的tag,实际上我觉得还没有另外一道make sorted的贪心类型的题目难。 阅读全文
posted @ 2019-08-07 23:13 fish1996 阅读(203) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/unique-paths/ 简单的dp题,类爬台阶题目。 阅读全文
posted @ 2019-08-07 22:49 fish1996 阅读(124) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/soup-servings 用的记忆搜索。第一次提交栈溢出了(虽然已经做了记忆)……之后在评论区发现了宝藏优化方法!以及发现自己没有必要返回pair,直接对情况2返回0.5就可以了。 阅读全文
posted @ 2019-08-07 22:32 fish1996 阅读(334) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/triangle/ 非常简单直观的dp,注意下标不要搞错就可以了。 阅读全文
posted @ 2019-08-07 21:07 fish1996 阅读(135) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/decode-ways 一道比较经典的爬楼梯类型的dp。 阅读全文
posted @ 2019-08-07 16:01 fish1996 阅读(136) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/number-of-longest-increasing-subsequence/ 这道题问的是最长的连续子序列的个数。这道题是XX子序列系列,是求最长连续子序列长度的升级版,关于求最长子序列长度,我们可以设定一个数组len 阅读全文
posted @ 2019-08-07 14:10 fish1996 阅读(146) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 以下这个答案超了内存限制orz,思路应该没啥问题,感觉要去评论区学一下花式优化技巧了。 补充·,仔细看了一下卡空间复杂度的竟然是因为有个很大的K,但有效 阅读全文
posted @ 2019-08-07 11:39 fish1996 阅读(156) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/mini-parser/ 一道普通的翻译题?代码写的很不优雅: 阅读全文
posted @ 2019-08-06 23:07 fish1996 阅读(141) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/binary-tree-level-order-traversal 二刷此题,现在看起来是基础中的基础题了。 阅读全文
posted @ 2019-08-06 22:25 fish1996 阅读(87) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/01-matrix/ 多源最短路径。这道题之前用深搜做的,复习宽搜的时候又用宽搜刷了一遍,以0为起点即可。 阅读全文
posted @ 2019-08-06 22:17 fish1996 阅读(133) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 非常基础的单源无权最短路径,没有任何变化。 阅读全文
posted @ 2019-08-06 22:03 fish1996 阅读(259) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/shortest-path-with-alternating-colors/ 周赛题,这道题是带约束的最短路径,我的做法是对不同颜色节点维护两个容器,分别计算从红色结点开始和从蓝色结点开始,取其中最小的。(这道题没有必要用优 阅读全文
posted @ 2019-08-06 20:08 fish1996 阅读(307) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/sliding-puzzle/ 一看到求最短次数就是宽搜了。用字符串来记录所有状态量即可,找到目标状态量后就退出,我们总能保证找到时用到的步数是最少的。 阅读全文
posted @ 2019-08-06 15:58 fish1996 阅读(125) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/snakes-and-ladders/ 这道题需要换算位置和数字之间的关系,虽然并不难但是很繁琐。 因为遇到了梯子/蛇一定要滑过去,所以我们可以当作把这一位置作为跳板直接到达了梯子末端,就好像没有来过这一位置一样。 阅读全文
posted @ 2019-08-06 14:39 fish1996 阅读(198) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/longest-consecutive-sequence/ 使用并查集,时间不是严格的O(n) 阅读全文
posted @ 2019-08-06 12:44 fish1996 阅读(225) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/word-ladder-ii/ 这道题做的好艰难,感觉还是经验不足导致的。 一个比较麻烦的地方是,我们需要使用一个path容器来记录访问过的路径,对于每个字符串而言,它可以由多个字符串转换而来,所以路径的上一个位置需要记录成 阅读全文
posted @ 2019-08-05 22:58 fish1996 阅读(172) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/longest-valid-parentheses/ 在动态规划中,我们希望尽可能利用已经求解好的子问题结果。 对于这道问题,我们可以假设我们已经知道了以第i个字符为结尾的最长有效括号个数为 x, 那么如果它的下一个字符( 阅读全文
posted @ 2019-08-05 16:05 fish1996 阅读(133) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 二刷此题了,发现第二次刷的时候几乎不需要思考就写出来了。之前虽然也做出了O(N)的解法,但是写起来很痛苦,因为之前是靠着人类天生 阅读全文
posted @ 2019-08-05 14:16 fish1996 阅读(121) 评论(0) 推荐(0)
摘要:问题:https://leetcode.com/problems/last-stone-weight-ii/ 01背包问题,记录不同容量的背包能否被恰好装满即可。 阅读全文
posted @ 2019-08-05 13:58 fish1996 阅读(324) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/random-pick-with-weight/ 首先计算一个累积的频数,根据总频数来进行随机,之后通过二分查找得到当前随机数对应的下标。 阅读全文
posted @ 2019-08-05 10:57 fish1996 阅读(114) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/trapping-rain-water/ 此题需要维护首尾两个指针,每次移动较小高度处的指针。 同时,维护左右的最大高度,作为当前可装水的高度。每次更新较小高度处的装水量,因为当前位置高度比另一侧更小,起码可以保证水不会从另 阅读全文
posted @ 2019-08-04 21:48 fish1996 阅读(343) 评论(0) 推荐(0)
摘要:刷了两个半月leetcode,成果如下,以medium为主,加了一些hard。easy不少都是比赛中做的,接下来可能不会大量刷新题了,开始进入总结阶段。总结还未开始,本篇文章先占一个坑。 2020年3月19日更新:本文是去年七月的时候写的,最近我又开始刷题了,so,这篇文章继续更新,更新了一下lee 阅读全文
posted @ 2019-08-04 21:33 fish1996 阅读(9249) 评论(2) 推荐(2)
摘要:problem:https://leetcode.com/problems/maximal-rectangle/ 我的做法是把它转为一维的。先计算每一列的前缀和。 之后按行扫描,可以把每一行看作横坐标,当前的值看作高度,得到一个直方图一样的东西,接下来只需计算直方图中的最大矩形面积。时间复杂度O(N 阅读全文
posted @ 2019-08-04 21:29 fish1996 阅读(361) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 这道题有好几个变种。对于没有cooldown的题目而言,只需要考虑第i天,当前持有股票的最大收益和当前不持有股票的最大收益。而引入 阅读全文
posted @ 2019-08-04 18:59 fish1996 阅读(157) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 维护两个状态,一个是当前持有股票状态,一个是当前不持有股票状态,两者分别计算最大值。 在第i天,如果当天不持有股票, 阅读全文
posted @ 2019-08-04 18:26 fish1996 阅读(199) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/rabbits-in-forest/ 值为n的可以和其它n + 1个值为n的成组,统计每个值出现的次数,看它们可以组成多少组相同颜色的兔子,然后乘以组中兔子个数。 阅读全文
posted @ 2019-08-04 17:44 fish1996 阅读(104) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/prefix-and-suffix-search/ 使用两个Trie树,分别维护前缀树和后缀树,快速查找到所有满足条件的前缀字符串和后缀字符串,再一一比较同时满足前缀和后缀的(同时出现在两个查找结果中),取下标最大的。 阅读全文
posted @ 2019-08-04 16:02 fish1996 阅读(182) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/snapshot-array/ 这道题暴力做(把所有快照都存下来)会超出空间限制,因此需要减少使用空间,两次快照直接没有改变的值就无需重复记录。可以使用二分搜索,每次修改值的时候记录一下当前快照的值,然后每次查找当前下标特定 阅读全文
posted @ 2019-08-04 14:22 fish1996 阅读(169) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/contest/weekly-contest-148/problems/decrease-elements-to-make-array-zigzag/ 贪心题。有两种可能的zigzag,一个是奇数下标为最大值,一个是偶数下标为最大值。分别就这 阅读全文
posted @ 2019-08-04 13:31 fish1996 阅读(379) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/contest/weekly-contest-148/problems/longest-chunked-palindrome-decomposition/ 周赛题。首尾两个指针,检测前后字符串是否相等,如果相等则两个指针都移动到下一个匹配位置 阅读全文
posted @ 2019-08-04 13:25 fish1996 阅读(254) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/ 有四种可能性,相等数字为A[0],出现在上面或下面;相等数字为B[0],出现在上面或下面。取这四者最小值即可。 阅读全文
posted @ 2019-08-03 20:04 fish1996 阅读(139) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/construct-quad-tree/ 一道简单的建树题目,模拟即可。 阅读全文
posted @ 2019-08-03 19:02 fish1996 阅读(156) 评论(0) 推荐(0)
摘要:problem: https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ O(n2), 把已有的a + b = c 结果用hashmap存起来,然后计算当前数字为结尾的最长类斐波那契子序列。 其中,dp[i][j] 阅读全文
posted @ 2019-08-03 18:11 fish1996 阅读(169) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/ 暴力枚举了一下所有子字符串可能对应的数字,统计是否达到N个,就过了。不知道有没有更快的方法。 阅读全文
posted @ 2019-08-03 17:14 fish1996 阅读(132) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/knight-dialer 可能是一个比较蠢的dp做法,因为我手动把跳转的方法写成了表: 阅读全文
posted @ 2019-08-03 17:04 fish1996 阅读(268) 评论(0) 推荐(0)
摘要:problem: 一道模拟题,用深搜匹配一下所有情况即可。 阅读全文
posted @ 2019-08-03 14:52 fish1996 阅读(213) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/car-fleet/ 按位置对车进行排序,然后从后往前扫描,判断当前车能否被后面一辆车超越(比较后面的车达到终点用的时间是否更短),如果能,以当前这辆车为基础,再判断下一辆后面的车能否超越它;否则以后面一辆车作为新的比较标准 阅读全文
posted @ 2019-08-03 10:55 fish1996 阅读(168) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/string-to-integer-atoi/ 这题题号挺前的,但一直没做。今天提了5次才过,终于知道这道题为什么通过率这么低了。主要就是if/else练习,尤其是完全没看题后会挂在很多地方。 阅读全文
posted @ 2019-08-02 23:30 fish1996 阅读(104) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/flatten-nested-list-iterator/ 这道题要手动维护栈,对于弱鸡的我写起来真痛苦Orz 阅读全文
posted @ 2019-08-02 22:42 fish1996 阅读(152) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/complete-binary-tree-inserter/ 使用队列来追踪当前可用的父节点,就可以实现快速插入。 阅读全文
posted @ 2019-08-02 16:31 fish1996 阅读(181) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/binary-tree-pruning/ 一道非常简单的tree题目,根据题意模拟剔除不包含0的子树即可。 阅读全文
posted @ 2019-08-02 12:04 fish1996 阅读(89) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/max-consecutive-ones-iii/ 维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来。不断更新当前滑动窗口中的数据个数,并取最大值返回即可。 阅读全文
posted @ 2019-08-02 12:01 fish1996 阅读(145) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/online-election/ 二分搜索题,主要是根据时间来二分,先预先计算好当前时间点对应的选举人,存到hashmap中。之后通过二分找到时间,再通过hash找到对应选举人。 阅读全文
posted @ 2019-08-01 21:48 fish1996 阅读(338) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/reveal-cards-in-increasing-order/ 我的思路先排序,然后反向模拟整个过程,按从大大小的顺序放入数据,每次放入之前,把最后一个数字挪到前面来。 阅读全文
posted @ 2019-08-01 20:37 fish1996 阅读(144) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/interleaving-string/ 我用的方法是记忆搜索,直接尝试模拟匹配的过程,速度比较慢。 阅读全文
posted @ 2019-08-01 20:21 fish1996 阅读(151) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/array-nesting/ 这道题有点类似于找最大的连通子图,只不过是以数组来表示的,难度并不高。 阅读全文
posted @ 2019-08-01 20:16 fish1996 阅读(127) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/sudoku-solver 一道用dfs纯模拟的题目,每个空白先填一个可能的数字(行列方格都没出现的),先一直这么尝试把所有空白填满,到了没有数字可填的时候,就回退到上一步。通过不断地尝试,最终“试”出最终的答案。 阅读全文
posted @ 2019-08-01 14:40 fish1996 阅读(159) 评论(0) 推荐(0)
摘要:problem:https://leetcode.com/problems/3sum-closest/ xxsum是一个系列的题了,一般有二分搜索和哈希两种方法,这道题要求的是接近值,所以哈希就不起作用了,只能使用二分搜索。 对于排好序的数组,首先确定三个数位置在中间的那个数字,然后在它左右搜索另外 阅读全文
posted @ 2019-08-01 11:30 fish1996 阅读(122) 评论(0) 推荐(0)