Re: [閒聊] 每日LeetCode

作者: wu10200512 (廷廷)   2024-01-30 20:42:02
為了下次codility不要烙賽
再寫一題
map真好用
49. Group Anagrams
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> ans;
unordered_map<string, int> mp;
for(const string& str:strs){
string sstr = str;
sort(sstr.begin(), sstr.end());
if(mp.count(sstr)){
ans[mp[sstr]].push_back(str);
}
else{
mp[sstr]=ans.size();
ans.push_back({str});
}
}
return ans;
}
};
作者: JIWP (JIWP)   2024-01-30 20:43:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com