leetcode
文章平均质量分 79
_Eric_Lan_
工科狗,略懂软硬件,尚在奋斗中
展开
-
leetcode 124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum题目描述找出尽可能使得和最大的路径(所有点需要连接)解题思路设计递归函数findPathSum(node),传入节点,节点为空时停止递归,返回到传入节点(左右子节点)为止积累的最大和。如图左半部分,每次计算三个值:sumLeft:根节点和左子树的和sumRight:根节点和右子树的和sumAll:根节点和左右子树的和计算当...原创 2019-11-05 14:38:25 · 160 阅读 · 0 评论 -
leetcode108/109 Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree题目描述给定一个有序升序数组,输出可能的平衡二叉树Example:Given the sorted array: [-10,-3,0,5,9]One possible answer is: [0,-3,9,-10,null,5] 0 / \ -3 9 ...原创 2019-11-05 12:43:23 · 175 阅读 · 0 评论 -
leetcode 143. Reorder List (medium)
题目描述Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You may not modify the values in the list’s nodes, only nodes itself may be changed.举个例子:0 1 2 3 4 ---->...原创 2019-08-15 11:50:11 · 111 阅读 · 0 评论 -
leetcode 144/145 Binary Tree Traversal (medium/hard)
144. Binary Tree Preorder Traversal (medium)https://leetcode.com/problems/binary-tree-preorder-traversal/题目描述用迭代的方式前序遍历二叉树, 而不使用递归.解题思路前序遍历的顺序为 中->左->右, 考虑左边节点的始终比右边节点的要先遍历, 因此可以用栈, 先进后出的特性...原创 2019-08-15 14:28:17 · 97 阅读 · 0 评论 -
leetcode 187. Repeated DNA Sequences
187. Repeated DNA Sequences题目描述简单来说就是找出一个字符串中 所有重复一次以上 的长度为10的子串Input: s = “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”Output: [“AAAAACCCCC”, “CCCCCAAAAA”]解题思路这种在长字符串中找字符串的题目, 要使用dictionary或者hashmap来保存数据,...原创 2019-08-24 14:01:26 · 153 阅读 · 0 评论 -
leetcode 146. LRU Cache (medium)
https://leetcode.com/problems/lru-cache/题目描述Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the val...原创 2019-08-15 16:56:00 · 134 阅读 · 0 评论 -
leetcode 151. Reverse Words in a String
https://leetcode.com/problems/reverse-words-in-a-string/题目描述Given an input string, reverse the string word by word.Example 1:Input: “the sky is blue”Output: “blue is sky the”解题思路用split()将string...原创 2019-08-20 20:27:57 · 143 阅读 · 0 评论 -
leetcode 134. Gas Station (Medium)
https://leetcode.com/problems/gas-station/非常经典的一道题。解这道题的思路基于一个数学定理:如果一个数组的总和非负,那么一定可以找到一个起始位置,从他开始绕数组一圈,累加和一直都是非负的有了这个定理,判断到底是否存在这样的解非常容易,只需要把全部的油耗情况计算出来看看是否大于等于0即可。那么如何求开始位置在哪?注意到这样一个现象:假如从位置i...原创 2019-08-12 21:53:58 · 175 阅读 · 0 评论 -
leetcod 152. Maximum Product Subarray (medium)
https://leetcode.com/problems/maximum-product-subarray/原创 2019-08-21 17:43:22 · 142 阅读 · 0 评论 -
leetcode 153. Find Minimum in Rotated Sorted Array
153. Find Minimum in Rotated Sorted Array题目描述Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).Fi...原创 2019-08-21 22:59:31 · 194 阅读 · 1 评论 -
leetcode 154. Find Minimum in Rotated Sorted Array II (hard)
154. Find Minimum in Rotated Sorted Array II题目描述Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2])....原创 2019-08-22 21:27:46 · 157 阅读 · 0 评论 -
leetcode 1-100 easy难度题目汇总
主要是考虑到easy题目没什么好分析的, 故统一总结在一篇文章里. 留作记录.收录leetcode1文章目录1. Two Sum (Easy)7. Reverse Integer (Easy)9. Palindrome Number (Easy)13. Roman to Integer (Easy)14. Longest Common Prefix (Easy)20. Valid Parenth...原创 2019-08-26 21:26:58 · 696 阅读 · 0 评论 -
leetcode 200. Number of Islands
200. Number of Islands题目描述1 代表岛屿, 0 代表海洋, 1 被 0 包围算作一个岛屿. 只要1被连接, 都只能算一个岛屿.解题思路用DFS解题. 遍历grid所有点, 如果grid[i][j]==1, 则对grid[i][j]由四个方向进行DFS, 把所有经过的点标记为x(表示去过), 所以每次遇到grid[i][j]==1的点一定是一个新的岛屿.代码Pyt...原创 2019-08-26 09:57:03 · 170 阅读 · 0 评论 -
leetcode 199. Binary Tree Right Side View
199. Binary Tree Right Side View题目描述假设站在二叉树右侧, 求能看到的节点.解题思路层序遍历, 每层的最后一个就是能被看到的.代码Python# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = ...原创 2019-08-26 09:16:57 · 140 阅读 · 0 评论 -
leetcode 198. House Robber (easy)
198. House Robber题目描述从一个数组中取(rob)若干数, 使得结果(money)最大, 这些数不能相邻Input: [1,2,3,1]Output: 4Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).Total amount you can rob = 1 + 3 = 4.解...原创 2019-08-26 08:53:19 · 164 阅读 · 0 评论 -
leetcode 144/145. Binary Tree Traversal
144. 中序遍历https://leetcode.com/problems/binary-tree-preorder-traversal/思路递归方法如下, 本题要求iretatively, 也就是用迭代的方式做.class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: ...原创 2019-08-26 21:38:36 · 183 阅读 · 0 评论 -
leetcode 1-100 medium难度题目汇总
2. Add Two Numbers (Medium)题目描述给两个链表, 做加法Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8Explanation: 342 + 465 = 807.解题思路"""类似于合并两个链表1. 依顺序把l2的元素加到l1中2. 当一个链表已经加完, ...原创 2019-09-07 12:49:52 · 1046 阅读 · 0 评论 -
leetcode 141/142 Linked List Cycle (easy)
141. Linked List Cyclehttps://leetcode.com/problems/linked-list-cycle/题目描述Given a linked list, determine if it has a cycle in it.Return True or False思路1最常规的思路,直接开辟一个list存放所有节点,每次都判断当前节点的子节点是否在li...原创 2019-08-15 11:05:53 · 130 阅读 · 0 评论 -
leetcode 179. Largest Number (medium)
179. Largest Number题目描述Given a list of non negative integers, arrange them such that they form the largest number.Input: [3,30,34,5,9]Output: “9534330”Note: The result may be very large, so you n...原创 2019-08-23 22:13:20 · 172 阅读 · 0 评论 -
leetcode 167. Two Sum II - Input array is sorted (easy)
167. Two Sum II - Input array is sorted题目描述Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.Note:默认只有一个解, 而且...原创 2019-08-23 19:22:42 · 365 阅读 · 1 评论 -
leetcode 101. Symmetric Tree
https://leetcode.com/problems/symmetric-tree/这道题很简单,用递归解决分情况讨论:当根节点为空时 返回True当根节点相等时 返回True递归调用isMirror(), 当node1.value == node2.value && isMirror(node1.left, node2.right) && isM...原创 2019-07-29 10:15:35 · 126 阅读 · 0 评论 -
leetcode 103. Binary Tree Zigzag Level Order Traversal
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/跟102题几乎一样,只不过加个反转。class Solution: def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]: if root == None...原创 2019-07-29 13:34:33 · 119 阅读 · 0 评论 -
leetcode 104. Maximum Depth of Binary Tree(easy)
非常简单,用递归,终止条件是当前结点为空的时候,返回0,每层返回能得到的最大深度+1。class Solution: def maxDepth(self, root: TreeNode) -> int: if root == None: return 0 return max(self.maxDepth(root.left),...原创 2019-07-29 13:54:10 · 116 阅读 · 0 评论 -
leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal (medium)
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/preorder [ 3 / 9 / 20 15 7 / ]inorder [ / 9 / 3 / 15 20 7 / ]preorder[0]就是根节点,此时inorder中该节点的左部分是属于左子树,右部分属...原创 2019-07-29 15:02:36 · 125 阅读 · 0 评论 -
leetcode110. Balanced Binary Tree (easy)
https://leetcode.com/problems/balanced-binary-tree/第一种方法,不但要保证当前节点左右子树平衡,而且他们的各自的孩子节点也要平衡。因此每一个节点都递归调用getDepth(),重复计算开销较大,时间复杂度为O(nlogn)。class Solution: def isBalanced(self, root: TreeNode) -&g...原创 2019-07-29 16:49:51 · 147 阅读 · 0 评论 -
leetcode 111. Minimum Depth of Binary Tree (easy)
https://leetcode.com/problems/minimum-depth-of-binary-tree/题目强调“The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.”题目强调必须找到叶子节点。递归的终止条件...原创 2019-07-29 21:47:31 · 132 阅读 · 0 评论 -
leetcode 112. Path Sum (easy)
思路和104,111,计算depth的题目递归思路一样。每一次递归,减掉当前节点的值。很简单。class Solution: def hasPathSum(self, root: TreeNode, sum: int) -> bool: if root == None: return False if ro...原创 2019-07-29 22:23:42 · 107 阅读 · 0 评论 -
leetcode 113. Path Sum II
https://leetcode.com/problems/path-sum-ii/基本思路和上一题一样,维护两个list,存储答案的ans和存储当前路径的path,用DFS优先搜索到叶子节点,如果符合sum要求,在ans中加入当前路径的list,如果对路径path使用append会改变path内容,所以此类问题,传参的时候一定要注意用+,不会改变原内容。 class Solution: ...原创 2019-07-30 11:08:38 · 104 阅读 · 0 评论 -
leetcode 114. Flatten Binary Tree to Linked List (medium)
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/用分治的思想。对于一个节点,lastLeft表示节点最左的叶子节点,lastRight表示最右的叶子节点,对每个节点执行以下三步操作:因为左子树的链接优先级都是高于右子树的,若lastLeft存在,执行lastLeft.left = root.right,若不存...原创 2019-07-30 15:17:23 · 125 阅读 · 0 评论 -
leetcode 115. Distinct Subsequences (hard)
https://leetcode.com/problems/distinct-subsequences/经典DP,归结为给两个数组,执行某些操作后,需要怎样。dp[i][j]表示目前为止 s[1:j] 中能匹配出多少个 T[1:i],绿色表示 t[i] = s[j] 的情况下有多少种可行解,是 s[1:j-1] 中匹配出 T[1:i-1] 的数量+不使用当前 s[j] 而匹配的 T[1:i-1...原创 2019-07-30 20:39:10 · 128 阅读 · 0 评论 -
leetcode 116. Populating Next Right Pointers in Each Node
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/层序遍历,只不过在遍历同一层的时候,每次将当前节点的next指向队列中下一个即将遍历的节点。class Solution: def connect(self, root: 'Node') -> 'Node': if r...原创 2019-07-30 21:35:34 · 104 阅读 · 0 评论 -
leetcode leetcode 120. Triangle
https://leetcode.com/problems/triangle/submissions/sumList用来保存上一层路径的求和结果,nextSumList保存当前层的求和结果,nextSumList[i] = row[i] + min(sumList[i-1], sumList[i]),当前值加上 上一层两个路径最小的和。class Solution: def minim...原创 2019-08-05 15:05:00 · 171 阅读 · 0 评论 -
leetcode 136. Single Number
https://leetcode.com/problems/single-number/submissions/题目描述:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear r...原创 2019-08-13 23:03:19 · 103 阅读 · 0 评论 -
leetcode 137. Single Number II
https://leetcode.com/problems/single-number-ii/题目描述:Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:要求时...原创 2019-08-14 16:19:44 · 165 阅读 · 0 评论 -
leetcode 165. Compare Version Numbers
165. Compare Version Numbers题目描述Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.You may assume that the...原创 2019-08-23 16:49:55 · 129 阅读 · 0 评论 -
leetcode 150. Evaluate Reverse Polish Notation (medium)
https://leetcode.com/problems/evaluate-reverse-polish-notation/题目描述Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an i...原创 2019-08-20 00:08:32 · 107 阅读 · 0 评论 -
leetcode 102. Binary Tree Level Order Traversal
https://leetcode.com/problems/binary-tree-level-order-traversal/很简单。维护三个数组,ans存放答案,curQueue存放当前层需要遍历的结点,他们的子节点统一存放在nextQueue,当curQueue为空时,将nextStack的结点移入,同时ans开辟一个新的数组,nextQueue置空。Python# Definiti...原创 2019-07-29 12:24:53 · 101 阅读 · 0 评论