Leetcode
Gareth_Hou
这个作者很懒,什么都没留下…
展开
-
[LeetCode题解] 面试题22. 链表中倒数第k个节点
前言:4月实在是太忙了,忙到几乎抽不出时间做题,也有工作级和专业级的编程都考过了的原因,5月做题继续,正好部门也有编码大赛。目前想法,把剑指offer刷完。题目链接: https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/.利用快慢指针,快指针先走K个元素,然后快慢指针一起往下走。走到头,慢...原创 2020-05-07 15:28:24 · 148 阅读 · 0 评论 -
[LeetCode题解] 84. 柱状图中最大的矩形
题目链接: https://leetcode-cn.com/problems/largest-rectangle-in-histogram/.本题目的直观思路是什么?遍历每一根柱子,由于组成矩形的高已知,我们向左右两边延伸,求矩形的最大长度。这种解法的时间复杂度为O(n^2)。单调栈做法:此做法借鉴Leetcode官方解答。其具体思路如下:维护一个单调递增的栈。遍历每一根柱子,当其高...原创 2020-03-21 20:30:19 · 139 阅读 · 0 评论 -
[LeetCode题解] 300. 最长上升子序列
题目链接: https://leetcode-cn.com/problems/longest-increasing-subsequence/.在Youtube,B站上搜了以下,关于O(nlogn)解法没有找到一讲我秒懂的tutorial,因此详细记录一下LIS 时间复杂度为O(nlogn)的做法。解法1:DP,通过双层遍历,如果num[i] > num[j],dp[i] = max(dp...原创 2020-03-14 22:54:55 · 295 阅读 · 0 评论 -
[LeetCode题解] 322. 零钱兑换
题目链接: https://leetcode-cn.com/problems/coin-change/.提供两种思路:解法一. DP执行用时 : 892 ms, 在所有 Python 提交中击败了77.35%的用户内存消耗 :12.1 MB, 在所有 Python 提交中击败了33.04%的用户class Solution(object): def coinChange(self...原创 2020-03-09 09:09:43 · 123 阅读 · 0 评论 -
[LeetCode题解] 994. 腐烂的橘子
题目链接: https://leetcode-cn.com/problems/rotting-oranges/.第一次做的时候,通过遍历grid来统计每一次好橘子的个数,由值为2的节点开始向外感染。如果好橘子的个数在t1和t2时间内一样,代表好橘子无法被遍历到,return -1。由于这种方法需要不断地遍历整个grid(统计好橘子数量),所以时间上损耗较大。class Solution(ob...原创 2020-03-04 19:41:49 · 108 阅读 · 0 评论 -
[LeetCode题解] 739. 每日温度
题目链接: https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/submissions/.该题的基本思路是维护一个单调栈。class Solution: def dailyTemperatures(self, T: List[int]) -> List[int]: ...原创 2020-03-01 15:35:40 · 120 阅读 · 0 评论 -
[LeetCode题解] 3. 无重复字符的最长子串
题目链接: https://leetcode-cn.com/problems/number-of-closed-islands/submissions/.这道题的思路与200.岛屿数量https://leetcode-cn.com/problems/number-of-islands/是一致的,都是通过DFS对于岛屿进行“感染”。class Solution(object): def ...原创 2020-01-24 16:39:32 · 199 阅读 · 0 评论 -
[LeetCode题解] 139. 单词拆分
题目链接: https://leetcode-cn.com/problems/word-break/.利用dp思想,以tmp_string = "watchdog"为例,如果tmp_string[:5] – watch 和 tmp_string[5:] --"dog"均在dict中,那么tmp_string必然可以被dict分解。class Solution(object): def ...原创 2020-01-20 20:01:59 · 148 阅读 · 0 评论 -
[LeetCode题解] 394. 字符串解码
题目链接: https://leetcode-cn.com/problems/decode-string/.这道题是我在公司模拟自测的时候遇到的一道题。参照题解区大佬的思路:由于括号内嵌套括号,与栈的先进后出一致。class Solution(object): def decodeString(self, s): """ :type s: str ...原创 2020-01-20 19:50:57 · 141 阅读 · 0 评论 -
[LeetCode题解] 102. 二叉树的层次遍历
题目链接: https://leetcode-cn.com/problems/binary-tree-level-order-traversal/comments/.自己的版本:class Solution(object): def levelOrder(self, root): """ :type root: TreeNode :rty...原创 2020-01-04 22:11:01 · 120 阅读 · 0 评论 -
[LeetCode题解] 199. 二叉树的右视图
题目链接: https://leetcode-cn.com/problems/increasing-order-search-tree/comments/.class Solution(object): def increasingBST(self, root): """ :type root: TreeNode :rtype: TreeN...原创 2020-01-04 21:38:55 · 118 阅读 · 0 评论 -
[LeetCode题解] 980. 不同路径 III
题目链接: https://leetcode-cn.com/problems/unique-paths-iii/comments/.第一次自己写的代码版本class Solution(object): def uniquePathsIII(self, grid): """ :type grid: List[List[int]] :rtyp...原创 2020-01-04 21:22:11 · 90 阅读 · 0 评论 -
[LeetCode题解] 921. 使括号有效的最少添加
题目链接: https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid/.第一次自己写的代码class Solution(object): def minAddToMakeValid(self, S): """ :type S: str :rtype: in...原创 2020-01-03 15:31:22 · 179 阅读 · 0 评论 -
[LeetCode题解] 145. 二叉树的后序遍历
题目链接: https://leetcode-cn.com/problems/binary-tree-postorder-traversal/.后序遍历原则:先遍历左子树,再遍历右子树,最后遍历根节点。递归版本:class Solution(object): def postorderTraversal(self, root): """ :type ...原创 2020-01-03 14:38:31 · 197 阅读 · 0 评论 -
[LeetCode题解] 111. 二叉树的最小深度
题目链接: https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/.第一次自己写的代码class Solution(object): min_depth = 2000 #理解为极大值即可 def minDepth(self, root): """ :type root: Tre...原创 2020-01-03 14:22:15 · 105 阅读 · 0 评论 -
[LeetCode题解] 107. 二叉树的层次遍历 II
题目链接: https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/.这道题与No.1302类似,都关注了树的层序遍历。class Solution(object): def levelOrderBottom(self, root): """ :type root: Tr...原创 2020-01-02 22:35:03 · 80 阅读 · 0 评论 -
[LeetCode题解] 1302.层数最深叶子节点的和
[LeetCode题解] 1302.层数最深叶子节点的和题目:https://leetcode-cn.com/problems/deepest-leaves-sum/这道题很明显是对于树的层序遍历。解法一:class Solution(object): def deepestLeavesSum(self, root): """ :type root: Tr...原创 2020-01-02 21:26:42 · 210 阅读 · 0 评论