ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

leetcode 884. Uncommon Words from Two Sentences 两句话中的不常见单词

leetcode 884. Uncommon Words from Two Sentences 两句话中的不常见单词

Problem: 884. Uncommon Words from Two Sentences 两句话中的不常见单词

解题过程

统计每个单词的频次,=1的就是答案

Code

class Solution { public: unordered_map<string, int> ump; vector<string> uncommonFromSentences(string s1, string s2) { s1 += " "; s2 += " "; string tg; for(int i = 0; i < s1.size(); i++) { if(s1[i]!=' ') { tg += s1[i]; } else { ump[tg]++; tg.clear(); } } tg.clear(); for(int i = 0; i < s2.size(); i++) { if(s2[i]!=' ') { tg += s2[i]; } else { ump[tg]++; tg.clear(); } } vector<string> ret; for(auto && [k, l] : ump) { if(l == 1) { ret.push_back(k); } } return ret; } };
返回列表