[leetcode] 884. Uncommon Words from Two Sentences

博客围绕找出两个句子中不常见单词的问题展开,介绍了三种算法。方法一用map记录单词出现次数;方法二和三用两个set分别记录出现过和重复的单词。此外,还总结了STL中set和map容器的初始化及常用操作函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

We are given two sentences A and B. (A sentence is a string of space separated words. Each word consists only of lowercase letters.)
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Return a list of all uncommon words.
You may return the list in any order.

Example 1:
Input: A = “this apple is sweet”, B = “this apple is sour”
Output: [“sweet”,“sour”]

Example 2:
Input: A = “apple apple”, B = “banana”
Output: [“banana”]

Note:
0 <= A.length <= 200
0 <= B.length <= 200
A and B both contain only spaces and lowercase letters.

方法一

算法思想:使用map记录每个单词出现的次数,最后取出所有出现一次的单词。
性能:0ms faster than 100%

		map<string,int> word;
        string s = A + " " + B;
        string temp;
        vector<string> res;
        map<string,int>::iterator it;
            
        stringstream a(s);
        while(a>>temp){
            if(word.count(temp)) word[temp]++;
            else word.insert(pair<string,int>(temp,1));
        }
        
        for(it = word.begin();it != word.end();it++) if(it->second == 1) res.push_back(it->first);
        return res;

方法二

算法思想:使用两个set分别记录出现过的单词和重复的单词,最后筛选出现过但不重复的单词
性能:4ms

		set<string> word,word2;
        string s = A + " " + B;
        string temp;
        
        stringstream a(s);
        while(a>>temp){
            if(word.find(temp) == word.end() && word2.find(temp) == word2.end()) word.insert(temp);
            else {
                word.erase(temp);
                word2.insert(temp);
            }
        }
        
        vector<string> res(word.begin(),word.end());
        return res;

方法三

算法思想:与方法二相同,只是用两个set记录出现过的单词(最早考虑的是用两个set的并集减去两个set的交集,但这样不能去掉在一个string中出现两次的单词)
性能:4ms

        set<string> s0,s1,s2;
        set<string>::iterator it;
        string temp;
        vector<string> res;
        
        stringstream a(A);
        while(a>>temp){
            if(s1.find(temp) != s1.end()) s0.insert(temp);
            else s1.insert(temp);
        }
        stringstream b(B);
        while(b>>temp){
            if(s2.find(temp) != s2.end()) s0.insert(temp);
            else s2.insert(temp);
        }
        
        for(it = s1.begin();it != s1.end();it++) if(s2.find(*it) == s2.end() && s0.find(*it) == s0.end()) res.push_back(*it);
        for(it = s2.begin();it != s2.end();it++) if(s1.find(*it) == s1.end() && s0.find(*it) == s0.end()) res.push_back(*it);
        
        return res;

STL:set

参考:STL中的set容器的一点总结

初始化:

set<int> s;

begin()    返回set容器的第一个元素

end()      返回set容器的最后一个元素

clear()    删除set容器中的所有的元素

empty()    判断set容器是否为空

max_size()   返回set容器可能包含的元素最大个数

size()      返回当前set容器中的元素个数

rbegin     返回的值和end()相同

rend()     返回的值和rbegin()相同

count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。

equal_range() ,返回一对定位器,分别表示第一个大于或等于给定关键值的元素和 第一个大于给定关键值的元素,这个返回值是一个pair类型,如果这一对定位器中哪个返回失败,就会等于end()的值。具体这个有什么用途我还没遇到过~~~

erase(iterator) ,删除定位器iterator指向的值

erase(first,second),删除定位器first和second之间的值

erase(key_value),删除键值key_value的值

find() ,返回给定值值得定位器,如果没找到则返回end()。

insert(key_value); 将key_value插入到set中 ,返回值是pair<set::iterator,bool>,bool标志着插入是否成功,而iterator代表插入的位置,若key_value已经在set中,则iterator表示的key_value在set中的位置。

inset(first,second);将定位器first到second之间的元素插入到set中,返回值是void.

lower_bound(key_value) ,返回第一个大于等于key_value的定位器

upper_bound(key_value),返回最后一个大于等于key_value的定位器

STL :map

参考资料:C++中的STL中map用法详解
初始化

// 初始化
map<string,int> wordCount;

//插入
wordCount.insert(pair<string,int>("string",1));

//遍历
map<string,int>::iterator it;
for(it = wordCount.begin();it != wordCount.end();it++){
	cout<<it->first<<' : '<<it->second<<endl;
}

//查找
wordCount.count("string"); // 有该属性时返回1,否则返回0
wordCount.find("string") != wordCount.end(); // 有该属性时返回iter,否则返回map.end()

// 删除
wordCount.erase("string");
wordCount.erase(it);
wordCount.erase(wordCount.begin(),wordCount.end());

map的基本操作函数:

 C++ maps是一种关联式容器,包含“关键字/值”对

 begin()         返回指向map头部的迭代器

 clear()        删除所有元素

 count()         返回指定元素出现的次数

 empty()         如果map为空则返回true

 end()           返回指向map末尾的迭代器

 equal_range()   返回特殊条目的迭代器对

 erase()         删除一个元素

 find()          查找一个元素

 get_allocator() 返回map的配置器

 insert()        插入元素

 key_comp()      返回比较元素key的函数

 lower_bound()   返回键值>=给定元素的第一个位置

 max_size()      返回可以容纳的最大元素个数

 rbegin()        返回一个指向map尾部的逆向迭代器

 rend()          返回一个指向map头部的逆向迭代器

 size()          返回map中元素的个数

 swap()           交换两个map

 upper_bound()    返回键值>给定元素的第一个位置

 value_comp()     返回比较元素value的函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值