摘要: 找BST里最近的值,用两次logn的搜索。注意递归过程中记录遇到过的closest。 看了题解可以不用递归,而且一次搜索中完成。 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # sel 阅读全文
posted @ 2020-02-05 23:18 阿牧遥 阅读(83) 评论(0) 推荐(0) 编辑
摘要: DFS搜索,使用prefix的字典加速;另外注意往result里放时要square.copy()一下 class Solution: def wordSquares(self, words: List[str]) -> List[List[str]]: wordsDict = {} for i in 阅读全文
posted @ 2020-02-05 22:58 阿牧遥 阅读(197) 评论(0) 推荐(0) 编辑
摘要: 用了一个字典 class Solution: def firstUniqChar(self, s: str) -> int: charIndexDict = {} for i in range(len(s)): if s[i] in charIndexDict: charIndexDict[s[i] 阅读全文
posted @ 2020-02-05 21:38 阿牧遥 阅读(107) 评论(0) 推荐(0) 编辑
摘要: 拓扑排序,可以用dfs做。也可以用bfs做(只有某节点的所有前驱都访问过了indegree--至0,才加入queue) class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bo 阅读全文
posted @ 2020-02-05 21:28 阿牧遥 阅读(240) 评论(0) 推荐(0) 编辑