随笔分类 -  LeetCode

上一页 1 2 3 4 5 6 7 ··· 13 下一页
刷过的题记录一下,思路仅供参考,并非最优解。
摘要:代码一:暴力超时了。 class Solution(object): # 暴力超时 def dailyTemperatures(self, T): """ :type T: List[int] :rtype: List[int] """ if not T: return [] res = [] fo 阅读全文
posted @ 2020-09-30 15:33 人间烟火地三鲜 阅读(125) 评论(0) 推荐(0)
摘要:代码一 class Solution(object): # 思路:用前序式DFS,从root到叶子。 def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if not root: return 阅读全文
posted @ 2020-09-30 15:25 人间烟火地三鲜 阅读(125) 评论(0) 推荐(0)
摘要:class Solution(object): def minReorder(self, n, connections): """ :type n: int :type connections: List[List[int]] :rtype: int """ # 记录所有点的出、入度:0表示入度,1 阅读全文
posted @ 2020-09-30 15:21 人间烟火地三鲜 阅读(157) 评论(0) 推荐(0)
摘要:class Solution(object): # 思路:BST的中序遍历应该是一个有序序列 def isValidBST(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True midList 阅读全文
posted @ 2020-09-30 15:17 人间烟火地三鲜 阅读(131) 评论(0) 推荐(0)
摘要:class Solution(object): def findSecondMinimumValue(self, root): """ :type root: TreeNode :rtype: int """ if not root: return -1 temp = [] stack = [roo 阅读全文
posted @ 2020-09-30 15:15 人间烟火地三鲜 阅读(106) 评论(0) 推荐(0)
摘要:class Solution(object): def isUnivalTree(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.dfs(root) def dfs 阅读全文
posted @ 2020-09-30 15:13 人间烟火地三鲜 阅读(76) 评论(0) 推荐(0)
摘要:class Solution(object): # 思路: # 先把句子按空格分成单词; # 遍历每个单词中的字符,查看是否是词根,是则替换并遍历下一个单词。 def replaceWords(self, dictionary, sentence): """ :type dictionary: Li 阅读全文
posted @ 2020-09-30 15:11 人间烟火地三鲜 阅读(142) 评论(0) 推荐(0)
摘要:一、数学法思路:题中说其他元素均出现三次。 class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ return (3 * sum(list(set(nums))) 阅读全文
posted @ 2020-09-30 15:08 人间烟火地三鲜 阅读(189) 评论(0) 推荐(0)
摘要:class Solution(object): def singleNumbers(self, nums): """ :type nums: List[int] :rtype: List[int] """ nums.sort() res = [] # 特判:处理第一个和最后一个元素 if nums[ 阅读全文
posted @ 2020-09-30 15:03 人间烟火地三鲜 阅读(127) 评论(0) 推荐(0)
摘要:代码一 class Solution(object): def checkIfExist(self, arr): """ :type arr: List[int] :rtype: bool """ if not arr: return True if arr.count(0) > 1: return 阅读全文
posted @ 2020-09-30 15:01 人间烟火地三鲜 阅读(153) 评论(0) 推荐(0)
摘要:代码一 用list或者字符串方式返回二叉树的所有路径:https://www.cnblogs.com/panweiwei/p/13752895.html class Solution(object): def pathSum(self, root, sum): """ :type root: Tre 阅读全文
posted @ 2020-09-30 14:57 人间烟火地三鲜 阅读(146) 评论(0) 推荐(0)
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS模板,百试不爽。 def listOfDepth(self, tree): """ 阅读全文
posted @ 2020-09-13 16:22 人间烟火地三鲜 阅读(107) 评论(0) 推荐(0)
摘要:class Solution(object): def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if not nums: return None # 二分法找nums中点 start, 阅读全文
posted @ 2020-09-13 16:19 人间烟火地三鲜 阅读(106) 评论(0) 推荐(0)
摘要:class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True # 求左子树深度 leftDenth = self.treeD 阅读全文
posted @ 2020-09-13 16:17 人间烟火地三鲜 阅读(97) 评论(0) 推荐(0)
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 思路:层序模板。 def maxDepth(self, root): """ :type 阅读全文
posted @ 2020-09-13 16:16 人间烟火地三鲜 阅读(106) 评论(0) 推荐(0)
摘要:方法一:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # BFS def kthLargest(self, root, k): """ :type 阅读全文
posted @ 2020-09-13 16:13 人间烟火地三鲜 阅读(109) 评论(0) 推荐(0)
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 层序模板,加个标记用来判断temp的正反序即可。 def levelOrder(self, roo 阅读全文
posted @ 2020-09-13 16:11 人间烟火地三鲜 阅读(121) 评论(0) 推荐(1)
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): # 思路: # 层序遍历,套用模板,百试不爽。 def levelOrder(self, root): 阅读全文
posted @ 2020-09-13 16:09 人间烟火地三鲜 阅读(74) 评论(0) 推荐(0)
摘要:BFS模板的应用。 总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html class Solution(object): def levelOrder(self, root): """ :type root: TreeNod 阅读全文
posted @ 2020-09-13 16:07 人间烟火地三鲜 阅读(75) 评论(0) 推荐(0)
摘要:class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.helper(root.left, 阅读全文
posted @ 2020-09-13 16:05 人间烟火地三鲜 阅读(96) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 7 ··· 13 下一页