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. koa2 get请求后台正常接收参数 前端报404错误

    刚学习一门技术时,总会踩一些坑. 前端代码 function del(mId){ $.ajax({ type:"get", url:"/delUser", da ...

  2. salesforce 零基础学习(七十)使用jquery tree实现树形结构模式

    项目中UI需要用到树形结构显示内容,后来尽管不需要做了,不过还是自己做着玩玩,mark一下,免得以后项目中用到. 实现树形结构在此使用的是jquery的dynatree.js.关于dynatree的使 ...

  3. MVC返回文件

    上一篇 介绍了Action 返回View, 顺便也看到了返回Json的处理, 这一篇并不看文件返回的源码, 此篇是为了应用. 1. Response返回文件 在MVC的项目中, 还是能看到很多同事, ...

  4. 散列算法-MD5

    信息摘要技术把明文内容按某种规则生成一段哈西值,即使明文消息只改动了一点点,生成的结果也会完全不同. MD5(Message-digest algorithm 5)就是信息摘要的一种实现,它可以从任意 ...

  5. 误删centos的ps命令,恢复

    脑子短路将/bin/ps文件删除.发现ps命令无法用了.下面是恢复方法 1,执行rpm -qf /bin/ps 查看ps命令所需要的rpm包 [root@iZm5e727lmif5lZ ~]# rpm ...

  6. 布局中的BFC---重点是前言

    一.前言 说实话,听到BFC这个概念我心里一阵咯噔,这到底是什么?有种似曾相识的感觉,但是又很模糊.问了一下度娘,看到张鑫旭的<CSS深入理解流体特性和BFC特性下多栏自适应布局>.呀,原 ...

  7. Entity framework 6.0 简明教程 ef6

    http://www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx Ef好的教程 Entity Fra ...

  8. Maven为不同环境配置打包

    在开发过程中经常要遇到为不同的环境打包,这里面最主要的问题在于,不同环境的配置是不一样的,如果为不同环境打包每次都手工修改配置,那不但工作量大,而且很容易出错.如果用ant的话,用变量加上replac ...

  9. C# 全文搜索Lucene

    全文出自:https://blog.csdn.net/huangwenhua5000/article/details/9341751 1 lucene简介1.1 什么是luceneLucene是一个全 ...

  10. Java虚拟机--Java内存区域的划分和异常

    Java内存区域的划分和异常 运行时数据区域 JVM在运行Java程序时候会将内存划分为若干个不同的数据区域. 程序计数器 线程私有.可看作是当前线程所执行的字节码的行号指示器,字节码解释器的工作是通 ...