Re: [閒聊] 每日leetcode
看板Marginalman作者enmeitiryous (enmeitiryous)時間1年前 (2024/09/17 08:15)推噓1(1推 0噓 1→)留言2則, 2人參與討論串869/1548 (看更多)
今天是easy 版主好像開票到四點真是辛苦了
題目: 884. Uncommon Words from Two Sentences
給你兩個字串s1 s2,找出其中只出現過一次的單字
思路:照做,用unordered map紀錄字串中空隔間開的單字出現次數最後只回傳出現過一次
的
vector<string> uncommonFromSentences(string s1, string s2) {
unordered_map<string,int> pre_ans;
string temp="";
for(int i=0;i<s1.size();++i){
if(s1[i]!=' '){
temp+=s1[i];
}
else{
pre_ans[temp]++;
temp="";
}
}
pre_ans[temp]++;
temp="";
for(int i=0;i<s2.size();++i){
if(s2[i]!=' '){
temp+=s2[i];
}
else{
pre_ans[temp]++;
temp="";
}
}
pre_ans[temp]++;
vector<string> ans;
for(auto k:pre_ans){
if(k.second==1){
ans.push_back(k.first);
}
}
return ans;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.167.20.227 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1726532128.A.21D.html
推
09/17 08:35,
1年前
, 1F
09/17 08:35, 1F
→
09/17 08:39,
1年前
, 2F
09/17 08:39, 2F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 869 之 1548 篇):