总的来说这周的题目较为简单,前两道完全是拼手速的题目。
5048. 拼写单词
给你一份『词汇表』(字符串数组) words
和一张『字母表』(字符串) chars
。
假如你可以用 chars
中的『字母』(字符)拼写出 words
中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。
注意:每次拼写时,chars
中的每个字母都只能用一次。
返回词汇表 words
中你掌握的所有单词的 长度之和。
class Solution(object):
def countCharacters(self, words, chars):
"""
:type words: List[str]
:type chars: str
:rtype: int
"""
dic = {}
for s in chars:
dic[s] = dic.get(s,0)+1
count = 0
res = 0
for s in words:
dic1 = {}
for i in range(len(s)):
dic1[s[i]] = dic1.get(s[i],0)+1
if s[i] in chars and dic1[s[i]]<=dic[s[i]]:
count += 1
if count == len(s):
res += count
count = 0
return res
5052. 最大层内元素和
给你一个二叉树的根节点 root
。设根节点位于二叉树的第 1
层,而根节点的子节点位于第 2
层,依此类推。
请你找出层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个。
class Solution(object):
def maxLevelSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
flag = 0
stack = [root]
max1 = 0
a = 0
while stack:
stack1 = []
res = 0
while stack:
tep = stack.pop()
if tep.left:
stack1.append(tep.left)
if tep.right:
stack1.append(tep.right)
res += tep.val
flag += 1
if res > max1:
max1 = res
a = flag
stack = stack1
return a