
# 331class Solution(object): def isValIDSerialization(self, preorder): stack = [] for node in preorder.split(','): stack.append(node) while len(stack) >= 3 and stack[-1] == stack[-2] == '#' and stack[-3] != '#': stack.pop(), stack.pop(), stack.pop() stack.append('#') return len(stack) == 1 and stack.pop() == '#'# 332class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: from collections import defaultdict graph = defaultdict(List) res = [] for x , y in sorted(tickets)[::-1]: graph[x].append(y) def dfs(tmp): while graph[tmp]: dfs(graph[tmp].pop()) res.append(tmp) dfs('JFK') return res[::-1]# 333class Solution: def largestBSTSubtree(self, root: TreeNode) -> int: def helper(root): if not root: return float('inf'), float('-inf'), 0 l_min, l_max, lv = helper(root.left) r_min, r_max, rv = helper(root.right) if l_max < root.val < r_min: return min(l_min, root.val), max(root.val, r_max), 1 + lv + rv return float('-inf'), float('inf'), max(lv, rv) return helper(root)[2]# 334class Solution: def increasingTriplet(self, nums: List[int]) -> bool: first , second = float('inf') , float('inf') for num in nums: if num > second: return True if first > num: first = num elif first < num < second: second = num return False# 335class Solution: def isSelfCrossing(self, x: List[int]) -> bool: if len(x) < 3: return False for i in range(3, len(x)): if x[i] >= x[i-2] and x[i-1] <= x[i-3]: return True if i >= 4 and x[i-1] == x[i-3] and x[i-4] + x[i] >= x[i-2]: return True if i >= 5 and x[i-3] >= x[i-5] and x[i-2] >= x[i-4] and x[i-3] - x[i-5] <= x[i-1] <= x[i-3] and x[i] + x[i-4] >= x[i-2]: return True return False # 336class Solution: def palindromePairs(self, words: List[str]) -> List[List[int]]: palIDStr = [] rev_words = {} res = [] for IDx, word in enumerate(words): rev_words[word[::-1]] = IDx if word[::-1] == word[:]: palIDStr.append(IDx) for IDx , word in enumerate(words): if word: for i in range(len(word)): left , right = word[:i] , word[i:] if left == left[::-1] and right in rev_words and rev_words[right] != IDx: res.append([rev_words[right] , IDx]) if right == right[::-1] and left in rev_words and rev_words[left] != IDx: res.append([IDx , rev_words[left]]) else: for loc in palIDStr: if loc != IDx: res.append([IDx , loc]) return res# 337class Solution: def rob(self, root: TreeNode) -> int: def helper(root): if not root: return 0, 0 left, prev1 = helper(root.left) right, prev2 = helper(root.right) return max(prev1 + prev2 + root.val, left + right), left + right return helper(root)[0]# 338class Solution: def countBits(self, num: int) -> List[int]: #res = [] #for i in range(num + 1): # res.append(str(bin(i)).count('1')) #return res 自己做的 dp = [0] * (num + 1) for i in range(1, num + 1): dp[i] = dp[i // 2] + (i % 2) return dp #看不懂# 340class Solution: def lengthOfLongestSubstringKdistinct(self, s: str, k: int) -> int: if len(s) == 0 or k == 0: return 0 dic = {} n = len(s) r , l = 0 , 0 res = 0 while r < n: dic[s[r]] = r r += 1 if len(dic) <= k: res = max(res , r - l) else: min1 = min(dic.values()) del dic[s[min1]] l = min1 + 1 return res总结
以上是内存溢出为你收集整理的python leetcode 331-340全部内容,希望文章能够帮你解决python leetcode 331-340所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)