上一页 1 2 3 4 5 6 7 ··· 43 下一页
摘要: 找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) 编辑
摘要: 利用了抛物线的性质,中间最小/最大,然后向/从两端逼近。 class Solution: def sortTransformedArray(self, nums: List[int], a: int, b: int, c: int) -> List[int]: result = [] if a == 阅读全文
posted @ 2020-02-04 12:43 阿牧遥 阅读(95) 评论(0) 推荐(0) 编辑
摘要: 思路简单,状态转移方程容易想。需要注意的是转移时的一些状态,比如是否连通,等等。 class Solution: def maxVacationDays(self, flights: List[List[int]], days: List[List[int]]) -> int: n = len(fl 阅读全文
posted @ 2020-02-03 23:14 阿牧遥 阅读(164) 评论(0) 推荐(0) 编辑
摘要: 用了前缀集合,高级的可以用前缀树 class Solution: def findWords(self, board: List[List[str]], words: List[str]) -> List[str]: if len(board) == 0 or len(board[0]) == 0: 阅读全文
posted @ 2020-02-03 00:17 阿牧遥 阅读(160) 评论(0) 推荐(0) 编辑
摘要: 首先用flooding,暴力的多次bfs,差一点就要超时 from queue import Queue class Solution: def markDistance(self, grid, i, j): m = len(grid) n = len(grid[0]) distance = [[N 阅读全文
posted @ 2020-02-01 21:20 阿牧遥 阅读(148) 评论(0) 推荐(0) 编辑
摘要: 用了python的set。为了效率,先做了预处理,并排序了。要注意,排序完才好预处理,否则i,j会对不上。 class Solution: def maxProduct(self, words: List[str]) -> int: maxProd = 0 words = sorted(words, 阅读全文
posted @ 2020-02-01 19:50 阿牧遥 阅读(90) 评论(0) 推荐(0) 编辑
摘要: 使用了queue from queue import Queue class MovingAverage: def __init__(self, size: int): """ Initialize your data structure here. """ self.que = Queue() s 阅读全文
posted @ 2020-02-01 18:04 阿牧遥 阅读(110) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 ··· 43 下一页