1. leetcode每日一题
class Solution:
def topStudents(self, positive_feedback: List[str], negative_feedback: List[str], report: List[str], student_id: List[int], k: int) -> List[int]:
# 哈希表,存储每一个positive_feedback或negative_feedback的分值,positive赋值3,negative赋值-1
word = {}
for w in positive_feedback:
word[w] = 3
for w in negative_feedback:
word[w] = -1
A = []
# 遍历每个学生的report和id
# 使用字典的get()来获取report中每一个单词的分数
# split将字符串按“空”分割成字符串列表
for s, i in zip(report, student_id):
score = sum(word.get(w, 0) for w in s.split())
A.append([-score, i])
A.sort() # 按照分数从大到小排列
# 返回前k个结果的id
return [i for v,i in A[:k]]
这个很好理解,但是还是直接看了题解。
感觉每日一题花费时间有点多,还是要把精力放在更重要的事情上。
split() 和 join()
zip
for s, i in zip(report, student_id):
相当于缝合吧,查书没查到。两个变量同步遍历两个list时候用