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 ...
随机推荐
- 【工具向01】——markdown 文本编辑语言相关
markdown简介 Markdown是一种轻量级标记语言创始人为约翰·格鲁伯.它允许人们"使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML或HTML文档".这种语言吸 ...
- Vue2.0-token权限处理
token一种身份的验证,在大多数网站中,登录的时候都会携带token,去访问其他页面,token就想当于一种令牌.可以判断用户是否登录状态.本次页面是通过Element-ui搭建的登录界面 当用户登 ...
- piplinedb 安装配置
piplinedb不能使用root用户操作,提前建好用户 [root@yzh-jkb-privatization /]# useradd steven [root@yzh-jkb-privatizat ...
- WebLogic 12c Linux 命令行 安装
最近负责在Linux上安装WebLogic Server 12c,客户说要安装最新的版本,版本号为 12.1.X(12.1.2,12.1.3).开始以为和旧版安装一样,使用控制台的方式,下载bin文件 ...
- Golang 并发concurrency
并发concurrency 很多人都是冲着Go大肆宣扬的高并发而忍不住跃跃欲试,但其实从源码解析来看,goroutine只是由官方实现的超级"线程池"而已.不过话说回来,每个实例4 ...
- 基于SpringBoot+SSM实现的Dota2资料库智能管理平台
Dota2资料库智能管理平台的设计与实现 摘 要 当今社会,游戏产业蓬勃发展,如PC端的绝地求生.坦克世界.英雄联盟,再到移动端的王者荣耀.荒野行动的火爆.都离不开科学的游戏管理系统,游戏管理系 ...
- vscode 自动提示Threejs
转自:https://blog.csdn.net/github_39125824/article/details/82633993 1.首先,你要安装Node.js 2.在vscode的 查看-> ...
- git撤销提交(commit)
我们知道Git有三大区(工作区.暂存区.版本库)以及几个状态(untracked.unstaged.uncommited) 一.简介 Git 保存的不是文件的变化或者差异,而是一系列不同时刻的文件快照 ...
- 在JAVA中封装JSONUtil工具类及使用
在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...
- Dubbox分布式框架
一:简介:前身是阿里巴巴的一个开源的项目,后来停止维护,由当当网继续维护,它致力于rpc远程的调度方案.是一个服务框架 二:执行原理图: 节点角色说明: · Provider: 暴露服务的服务提供方. ...