Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-09-17 18:31:27
※ 引述《enmeitiryous (enmeitiryous)》之铭言:
: 今天是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;
: }
思路:
照做 太久没刷题 前面变量还打错 debug搞了五分钟才发现
Python Code:
class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
l1 = s1.split()
l2 = s2.split()
result = []
for w in l1:
if w not in l2 and l1.count(w) == 1:
result.append(w)
for w in l2:
if w not in l1 and l2.count(w) == 1:
result.append(w)
return result

Links booklink

Contact Us: admin [ a t ] ucptt.com