【LeetCode】第235场
链接
1.截断句子
直接遍历统计空格数量
class Solution {
public:
string truncateSentence(string s, int k) {
int cnt = 0;
string res = "";
for(int i = 0;i < s.size(); i++){
if(s[i] == ' ') cnt++;
if(cnt == k) break;
res += s[i];
}
return res;
}
};
或用python一行搞定
class Solution:
def truncateSentence(self, s: str, k: int) -> str:
return ' '.join((s.split())[:k])
2.查找用户活跃分钟数
class Solution {
public:
vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {
unordered_map<int,unordered_set<int>> m;
for(auto &log : logs){
m[log[0]].insert(log[1]);
}
vector<int> res(k);
for(auto &i : m){
res[i.second.size()-1]++;
}
return res;
}
};