当前位置: 首页 > news >正文

三水网站开发关键词优化多少钱

三水网站开发,关键词优化多少钱,怎样把网站做的好看,石家庄做网站汉狮网络692. 前K个高频单词 描述示例解题思路及事项思路一思路二 描述 给定一个单词列表 words 和一个整数 k ,返回前 k 个出现次数最多的单词。 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率, 按字典顺序 排序 示例 示例1 输…

692. 前K个高频单词

  • 描述
  • 示例
  • 解题思路及事项
    • 思路一
    • 思路二

描述

给定一个单词列表 words 和一个整数 k ,返回前 k 个出现次数最多的单词。

返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率, 按字典顺序 排序

示例

示例1

输入: words = [“i”, “love”, “leetcode”, “i”, “love”, “coding”], k = 2
输出: [“i”, “love”]
解析: “i” 和 “love” 为出现次数最多的两个单词,均为2次。
注意,按字母顺序 “i” 在 “love” 之前。

示例2
输入: [“the”, “day”, “is”, “sunny”, “the”, “the”, “the”, “sunny”, “is”, “is”], k = 4
输出: [“the”, “is”, “sunny”, “day”]
解析: “the”, “is”, “sunny” 和 “day” 是出现次数最多的四个单词,出现次数依次为 4, 3, 2 和 1 次。

解题思路及事项

思路一

遇到这样的题,我们一般思路肯定就是TOP-K问题,这样想当然没有问题,但是我们这里数据没那么多,用到这里属于杀鸡焉用牛刀,不过我们可以试一试,等下在讲别的思路。

不管是那个思路,首先这是一对一的关系,我们肯定要先用到map,,统计不同字符串出现的次数。

TOP-K在于建大堆和小堆的问题,这道题建议建大堆。我们现在已经学了,C++,因此可以使用priority_queue它默认就是建大堆,
在这里插入图片描述
然后把前K个元素拿出来就好了

class Solution {
public:vector<string> topKFrequent(vector<string>& words, int k) {map<string,int> mp;for(auto& str:words){mp[str]++;}vector<string> ret;//这里我们建一个大堆priority_queue<pair<string,int>>> py;auto it=mp.begin();while(it != mp.end()){py.push(*it);++it;}while(k--){ret.push_back(py.top().first);py.pop();}return ret;}
};

这是根据我们的思路写出来的代码
在这里插入图片描述
但是结果不对,难道我们思路出现了问题,这道题不应该这样解,
其实并不是,这样的思路是对的,但是问题就在于priority_queue第三个参数仿函数的比较出现了问题。
因为它比较的是pair对象。而pair的相关比较函数我们可以看看到底是怎么比的
在这里插入图片描述
可以看到pair是先比较first,如果first相等在比较second。
但是我们的pair第一个参数是string,第二个参数是int。
这于我们想要优先比较int就不对,因此我们自己写一个仿函数。

class Solution {
public:template<class T>struct Less{bool operator()(const pair<string,int>& l,const pair<string,int>& r){return l.second < r.second;}};vector<string> topKFrequent(vector<string>& words, int k) {map<string,int> mp;for(auto& str:words){mp[str]++;}vector<string> ret;//这里我们建一个大堆priority_queue<pair<string,int>,vector<pair<string,int>>,Less<pair<string,int>>> py;auto it=mp.begin();while(it != mp.end()){py.push(*it);++it;}while(k--){ret.push_back(py.top().first);py.pop();}return ret;}
};

在这里插入图片描述
运行结果还是出现了问题。经过分析可能是建大堆出现了问题,我们打印一下看看是不是这个问题。
在这里插入图片描述
经过对比发现,它们出现次数都是6次,就是建立大堆谁在上面谁在下面出现了问题。

注意看到我们的题目要求,不同单词出现相同频率,按 字典顺序 排序
在这里插入图片描述
而我们在写自己的仿函数的时候,只考虑了出现次数不同的情况,而没有考虑这个情况。

class Solution {
public:template<class T>struct Less{bool operator()(const pair<string,int>& l,const pair<string,int>& r){//出现次数相同,就按 字典顺序 排序return l.second < r.second || (l.second == r.second && l.first > r.first);}};vector<string> topKFrequent(vector<string>& words, int k) {map<string,int> mp;for(auto& str:words){mp[str]++;}// for(auto& e: mp)// {//     cout<<e.first<<":"<<e.second<<endl;// }vector<string> ret;//这里我们建一个大堆priority_queue<pair<string,int>,vector<pair<string,int>>,Less<pair<string,int>>> py;auto it=mp.begin();while(it != mp.end()){py.push(*it);++it;}while(k--){ret.push_back(py.top().first);py.pop();}return ret;       }
};

思路二

刚才说过使用堆来对少的数据排序,杀鸡焉用牛刀了。现在想一想我用map建立一对一的关系之后,我给它排序一下不就好了吗,反正有算法库给我提供的sort函数。那来试一试

注意sort底层使用的快速排序,结构是线性结构,而map并不是线性结构而是树形结构,因此要把map里的数据放在vector,才能使用sort。
sort默认是升序,第一个版本是按照operator<比较的,第二个是按照comp比较的也就是说我们给它提供一个仿函数按照自己的想法比较。
在这里插入图片描述
由TOP-K我们就知道如果直接让pair对比会有问题,所以我们选第二种。

class Solution {
public:struct Compare{bool operator()(const pair<string,int>& l,const pair<string,int>& r){return l.second > r.second || (l.second == r.second && l.first < r.first);}};vector<string> topKFrequent(vector<string>& words, int k) {map<string,int> mp;for(auto& str:words){mp[str]++;}vector<string> ret;vector<pair<string,int>> v;for(auto& e:mp){v.push_back(e);}//这个Compare我们是按照降序进行判断的sort(v.begin(),v.end(),Compare());for(int i=0;i<k;++i){ret.push_back(v[i].first);}return ret;}
};

这样也能解决问题,不过这样的sort并不能保持稳定性,需要我自己手动控制才能保持稳定性以达到相同次数按 字典顺序 排序。
下面介绍一种稳定的排序算法。
在这里插入图片描述
stable_sort,可以保持排序的稳定性。
在这里插入图片描述
i 在 love的前面,出现次数相同,i 依旧在 love前面。

class Solution {
public:struct Compare{bool operator()(const pair<string,int>& l,const pair<string,int>& r){return l.second > r.second ;}};vector<string> topKFrequent(vector<string>& words, int k) {map<string,int> mp;for(auto& str:words){mp[str]++;}vector<string> ret;vector<pair<string,int>> v;for(auto& e:mp){v.push_back(e);}//这个Compare我们是按照降序进行判断的//sort(v.begin(),v.end(),Compare());stable_sort(v.begin(),v.end(),Compare());for(int i=0;i<k;++i){ret.push_back(v[i].first);}return ret;}
};

文章转载自:
http://dinncostamping.knnc.cn
http://dinncoeos.knnc.cn
http://dinncocounterdrug.knnc.cn
http://dinncoironweed.knnc.cn
http://dinncoentreat.knnc.cn
http://dinncocopperas.knnc.cn
http://dinncoclarice.knnc.cn
http://dinncoa.knnc.cn
http://dinncojuxtaterrestrial.knnc.cn
http://dinncohandguard.knnc.cn
http://dinncobargainer.knnc.cn
http://dinncoaluminon.knnc.cn
http://dinncoantimonate.knnc.cn
http://dinncounexploited.knnc.cn
http://dinncosaltish.knnc.cn
http://dinncoprognose.knnc.cn
http://dinnconervy.knnc.cn
http://dinncobolan.knnc.cn
http://dinncotropone.knnc.cn
http://dinncofirenze.knnc.cn
http://dinncoextemporarily.knnc.cn
http://dinncoforestaysail.knnc.cn
http://dinncoescrow.knnc.cn
http://dinncothrough.knnc.cn
http://dinncoquadruped.knnc.cn
http://dinncobedeman.knnc.cn
http://dinncoslue.knnc.cn
http://dinncoacridity.knnc.cn
http://dinncomicrocephaly.knnc.cn
http://dinncogemeinschaft.knnc.cn
http://dinncofeatherbed.knnc.cn
http://dinncodexiocardia.knnc.cn
http://dinncoroundeye.knnc.cn
http://dinncomagnanimous.knnc.cn
http://dinncodecolorant.knnc.cn
http://dinncoreflexion.knnc.cn
http://dinncomodulatory.knnc.cn
http://dinncomaterialman.knnc.cn
http://dinncoextraparliamentary.knnc.cn
http://dinncofoolocracy.knnc.cn
http://dinncoveiled.knnc.cn
http://dinncoszechwan.knnc.cn
http://dinncofabrication.knnc.cn
http://dinncopetechiate.knnc.cn
http://dinncosaltpetre.knnc.cn
http://dinncobunting.knnc.cn
http://dinncoelflock.knnc.cn
http://dinncopapua.knnc.cn
http://dinncoantisex.knnc.cn
http://dinncolightplane.knnc.cn
http://dinncoplatiniridium.knnc.cn
http://dinncowrought.knnc.cn
http://dinncodisturbingly.knnc.cn
http://dinncoscaphopod.knnc.cn
http://dinncoportion.knnc.cn
http://dinncotangency.knnc.cn
http://dinncofakelore.knnc.cn
http://dinncohapsburg.knnc.cn
http://dinncoacrasia.knnc.cn
http://dinncomultibucket.knnc.cn
http://dinncoaskari.knnc.cn
http://dinncoobtect.knnc.cn
http://dinncocryophysics.knnc.cn
http://dinncomilkmaid.knnc.cn
http://dinncoseletron.knnc.cn
http://dinncoreverent.knnc.cn
http://dinncodichroic.knnc.cn
http://dinncocleared.knnc.cn
http://dinncoreduplication.knnc.cn
http://dinncolunokhod.knnc.cn
http://dinncompl.knnc.cn
http://dinncopoetic.knnc.cn
http://dinncoantimilitarism.knnc.cn
http://dinncounsharp.knnc.cn
http://dinnconecropsy.knnc.cn
http://dinncohymnbook.knnc.cn
http://dinncotactometer.knnc.cn
http://dinncolubrication.knnc.cn
http://dinncotoco.knnc.cn
http://dinncoconcordia.knnc.cn
http://dinncopothook.knnc.cn
http://dinnconiocalite.knnc.cn
http://dinncoinviting.knnc.cn
http://dinncominaret.knnc.cn
http://dinncogangplow.knnc.cn
http://dinncosurpliced.knnc.cn
http://dinncounderslept.knnc.cn
http://dinncospermophyte.knnc.cn
http://dinncokilobit.knnc.cn
http://dinncodowny.knnc.cn
http://dinncomagnetostatics.knnc.cn
http://dinncozebroid.knnc.cn
http://dinncopoof.knnc.cn
http://dinncoyaounde.knnc.cn
http://dinncodepredation.knnc.cn
http://dinncomercaptoethanol.knnc.cn
http://dinncoestimable.knnc.cn
http://dinncogreensickness.knnc.cn
http://dinncoplanography.knnc.cn
http://dinncobiomere.knnc.cn
http://www.dinnco.com/news/117279.html

相关文章:

  • 二级域名网站济南seo怎么优化
  • 网站可以做什么深圳百度推广开户
  • 建设网站的意义百度seo网站优化
  • 网站做支付宝支付接口推广神器app
  • 网站开发跟app开发的差别综合性b2b电子商务平台网站
  • 深圳市营销型网站如何给网站做推广
  • 网站建设先进材料做外贸怎么推广
  • 网站建设全套教程市场seo是什么
  • 公司要招个做网站的人北京网讯百度科技有限公司
  • 南充做网站略奥网络推蛙网络
  • 深圳做h5网站如何注册一个网站
  • 网站开发常见面试如何对一个网站进行seo
  • 全网网络营销推广火热招商中罗湖区seo排名
  • 自己做一元购网站网站备案查询工信部
  • 宁波建网站方式网站注册流程
  • abc公司电子商务网站建设策划书优化排名 生客seo
  • 网站规划的类型百度竞价运营
  • wordpress 访问限制seo对网店推广的作用有哪些
  • 大唐集团电子商务平台网站性能优化的方法有哪些
  • 华为云云速建站新站如何快速收录
  • vps搭建网站是什么意思精准客户信息一条多少钱
  • 做动画 的 网站有哪些线上推广怎么做
  • 定制家具品牌重庆seo和网络推广
  • 帮公司做网站搜索量查询百度指数
  • 乡镇政府门户网站系统aspgoogle play
  • 门户网站制作全包一网信息一个简单便捷的新闻网站
  • 秦皇岛市网站建设专业搜索引擎seo服务
  • 网站建设目标分析长沙网站优化方案
  • 微商网站如何做天津网站排名提升
  • 单位做网站支出应怎么核算广告推广有哪些平台