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的更多相关文章

  1. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

  2. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

  3. 【LeetCode】819. Most Common Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...

  4. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. LeetCode算法题-Most Common Word(Java实现)

    这是悦乐书的第321次更新,第342篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第190题(顺位题号是819).给定一个段落和一组禁止词,返回不在禁止词列表中的最常用词 ...

  6. LeetCode题解之 Longest Common Prefix

    1.题目描述 2.问题分析 直接使用循环解决问题 3.代码 string longestCommonPrefix(vector<string>& strs) { string re ...

  7. 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 ...

  8. leetcode Most Common Word——就是在考察自己实现split

    819. Most Common Word Given a paragraph and a list of banned words, return the most frequent word th ...

  9. 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 ...

随机推荐

  1. Jmeter测试Mysql数据库-入门篇

    一.jmter配置数据库 1.在配置jmter之前需要先安装数据库连接池驱动,进入到官方下载页面https://dev.mysql.com/downloads/connector/j/,下载对应的驱动 ...

  2. JavaSE-序列化和反序列化

    什么是序列化,什么时候要进行序列化? 序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化,将数据分解成字节流,以便存储在文件中或在网络上传输. 我们在对java对象进行IO流操作 ...

  3. sqlserver暂时禁用触发器进行update

    --1.禁用某个表上的所有触发器 ALTER TABLE tbname DISABLE TRIGGER all go --2.执行update语句 update tbname set .... go ...

  4. php获取 POST请求的数据

    普通键值对的数据: $_POST[‘username’]; // 获取 username的信息: $_REQUEST; //则会获取 整个请求中的键值对,返回结果为数组: 如果是,流数据,则需要使用: ...

  5. linux----之tcpdump小用

    1.通过抓包大致确定访问量 #time tcpdump -nn -i eth0 'tcp[tcpflags] = tcp-syn' -c 10000 >/dev/null 根据real时间判断访 ...

  6. 网络之Json生成解析

    // // ViewController.m // Json // // Created by City--Online on 15/4/28. // Copyright (c) 2015年 CYW. ...

  7. [转]bootstrap栅格系统的属性及使用

    本文转自:https://www.cnblogs.com/LJYqq/p/6791512.html 栅格系统 媒体查询 在栅格系统中,我们在 Less 文件中使用以下媒体查询(media query) ...

  8. 在MVC应用程序中使用jQuery的验证

    呵呵,觉得很久没有写博客了,均是工作忙于公司的ERP系统,这是正确的,因为这才是真正的工作. 今天想写点在MVC应用程序中,使用jQuery来验证.在进行之前,还是先回看一下<MVC会员注册&g ...

  9. Spring基础(1) : 自动装配

    1.自动装配 1.1 byType 1.1.1根据类型自动匹配,若当前没有类型可以注入或者存在多个类型可以注入,则失败.必须要有对于的setter方法 public class Person{ pub ...

  10. MFC宏—DECLARE_DYNCREATE

    DECLARE_DYNCREATE( class_name ) 参数: class_name 类的实际名字(不用引号括起来). 说明: 使用DECLARE_DYNCREATE宏可以使每个CObject ...