LeetCode 题解之Most Common Word
1、题目描述

2、题目分析
首先将输入句子拆分成单词,在这个过程中将所有大写字母变换成小写字母,将每一个单词作为一个字符串放入一个 map<string,int> 容器中,最后遍历容器,查找出现次数最多且没有在banned中出现的字符串。
3、代码
string mostCommonWord(string paragraph, vector<string>& banned) {
map<string,int> m;
for( string::iterator it = paragraph.begin() ; it != paragraph.end(); ++it ){
if( isalpha(*it) ){
*it = std::tolower(*it) ;
auto it_i = it ;
while( it_i != paragraph.end() && isalpha( *it_i ) ){
*it_i = std::tolower( *it_i );
++it_i;
}
string s( it,it_i );
m[s]++;
it = (it_i == paragraph.end() )?it_i - : it_i ;
}
}
int count = ;
string result;
for( auto it_m = m.begin(); it_m != m.end(); ++it_m ){
if( find( banned.begin(), banned.end(),it_m->first ) == banned.end() && it_m->second > count ){
result = it_m->first;
count = it_m->second;
}
}
return result ;
}
LeetCode 题解之Most Common Word的更多相关文章
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- 《LeetBook》leetcode题解(14):Longest Common Prefix[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode算法题-Most Common Word(Java实现)
这是悦乐书的第321次更新,第342篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第190题(顺位题号是819).给定一个段落和一组禁止词,返回不在禁止词列表中的最常用词 ...
- LeetCode题解之 Longest Common Prefix
1.题目描述 2.问题分析 直接使用循环解决问题 3.代码 string longestCommonPrefix(vector<string>& strs) { string re ...
- LeetCode 819. Most Common Word (最常见的单词)
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- leetcode Most Common Word——就是在考察自己实现split
819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
随机推荐
- Glide图片加载库的使用
http://www.cnblogs.com/whoislcj/p/5558168.html这篇文章写得很详细,我这里简单说一些 1.app的build.gradle的dependencies里面添加 ...
- hbase和zookeeper的安装和部署
1.概要 1.1HBase的使用场景 大数据量 (100s TB级数据) 且有快速随机访问的需求. 例如淘宝的交易历史记录.数据量巨大无容置疑,面向普通用户的请求必然要即时响应. 容量的优 ...
- redis学习(三)redis持久化
redis持久化 1.redis持久化介绍 我们知道redis性能之所以强悍,是因为redis在运行时将数据都存放在了访问效率远高于硬盘的内存之中.可是这带来了新的问题:在redis或者外部系统重启时 ...
- Iptables之recent模块小结
Iptables的recent模块用于限制一段时间内的连接数, 是谨防大量请求攻击的必杀绝技! 善加利用该模块可充分保证服务器安全. recent常用参数--name 设定列表名称,即设置跟 ...
- Golang 反射reflection
反射reflection 反射可大大提高程序的灵活性,使得interface{}有更大的发挥余地 反射使用TypeOf和ValueOf函数从接口中获取目标对象信息 反射会将匿名字段作为独立字段(匿名字 ...
- Spark2.1.0——剖析spark-shell
在<Spark2.1.0——运行环境准备>一文介绍了如何准备基本的Spark运行环境,并在<Spark2.1.0——Spark初体验>一文通过在spark-shell中执行wo ...
- 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 热更新Live2D
live2D是一个很强大的2D动画组件.我们可以使用AS3脚本对它进行热更新. live2D在Unity中的使用请看这里: 如何获取Live2D 总得来说,我们可以先去live2D官网下载它的Unit ...
- iOS开源项目周报0413
由OpenDigg 出品的iOS开源项目周报第十六期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. glidin ...
- MVC实现更新数据库的数据
经过一系列MVC的练习,如数据库的数据显示,添加,删除等,还差一个功能,就是更新,那本次练习的是MVC对数据库的数据进行更新. 写好更新的存过程: 有了存储过程,可以写实体(Entity)的方法了.写 ...
- 微信小程序入门篇
微信小程序入门篇: 准备工作 IDE搭建 就不多说了,没有内测码去下载个破解版吧,我用了一下,学习完全够了!IDE破解版+安装教程 图片发自简书App 知识准备 JavaScrip还是要看看的,推荐教 ...