当前位置:首页 > 编程笔记 > 正文
已解决

Day13力扣打卡

来自网友在路上 164864提问 提问时间:2023-10-29 01:46:48阅读次数: 64

最佳答案 问答题库648位专家为你答疑解惑

打卡记录

在这里插入图片描述

奖励最顶尖的 k 名学生(哈希表+排序)

用哈希表对所有的positive与negative词条进行映射,然后遍历求解。tip:常用的分割字符串的操作:1.stringstream配合getline() [格式buf, string, char]2.string.find()[find未找到目标会返回npos]配合string.substr()

class Solution {
public:vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k) {unordered_set<string> pos, neg;for (auto& s : positive_feedback) pos.insert(s);for (auto& s : negative_feedback) neg.insert(s);vector<pair<int, int>> arr;int n = student_id.size();for (int i = 0; i < n; ++i) {stringstream ss;ss << report[i];string tmp;int res = 0;while (getline(ss, tmp, ' ')) {if (pos.count(tmp)) res += 3; else if (neg.count(tmp)) res--;}arr.push_back({-res, student_id[i]});}sort(arr.begin(), arr.end());vector<int> ans(k);for (int i = 0; i < k; ++i) ans[i] = arr[i].second;return ans;}
};
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Day13力扣打卡":http://eshow365.cn/6-27285-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!