python leetcode 331-340
作者:互联网
# 331
class 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() == '#'
# 332
class 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]
# 333
class 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]
# 334
class 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
# 335
class 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
# 336
class 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
# 337
class 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]
# 338
class 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 #看不懂
# 340
class 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
标签:right,return,python,res,331,340,root,stack,def 来源: https://blog.csdn.net/aaa12389zhx/article/details/116458289