摘要:
使用栈 class Solution: def lengthLongestPath(self, input: str) -> int: input = input + '\n' # add trailing dummy \n stack = [] level = 1 current = '' res 阅读全文
摘要:
正确做法是: 只保存行/列/对角线的和,而不用保存所有元素,空间复杂度从O(n2)降到O(n);move()只需判断四个值是否 = n - 1: 当前行sum,当前列sum,两个对角线,时间复杂度为O(1) 而我只是用dict记录是否还有可能赢。差强人意。 class TicTacToe: def 阅读全文
摘要:
用了并查集,分成有入度为2以及有环两种情况。 class Solution: def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]: n = len(edges) indegrees = {k + 阅读全文
摘要:
简单题。注意python的s[:-1]在s == ''时,也是生效的。(返回'') class Solution: def backspaceCompare(self, S: str, T: str) -> bool: normS = self.normalize(S) normT = self.n 阅读全文
摘要:
简单题 class Solution: def numUniqueEmails(self, emails: List[str]) -> int: result = set() for email in emails: splits = email.split('@') name = splits[0 阅读全文
摘要:
用dp会超时。其实一遍循环就可以。 这段代码写的比较烂,其实如果分len(s)==len(t)和len(t)-1两种情况,就简单了。 class Solution: def isOneEditDistance(self, s: str, t: str) -> bool: if len(s) > le 阅读全文
摘要:
简单题 class Solution: def imageSmoother(self, M: List[List[int]]) -> List[List[int]]: m = len(M) n = len(M[0]) result = [[0] * n for _ in range(m)] for 阅读全文
摘要:
排序。更naive的方法是用set。 class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [] nums1.sort() nums2.sort() i = 阅读全文