作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/occurrences-after-bigram/

题目描述

Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second.

For each such occurrence, add "third" to the answer, and return the answer.

Example 1:

Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
Output: ["girl","student"]

Example 2:

Input: text = "we will we will rock you", first = "we", second = "will"
Output: ["we","rock"]

Note:

  1. 1 <= text.length <= 1000
  2. text consists of space separated words, where each word consists of lowercase English letters.
  3. 1 <= first.length, second.length <= 10
  4. first and second consist of lowercase English letters.

题目大意

找出在一个长字符串text中,符合"first second third"模式的third。其中first和second是输入的。

解题方法

字符串分割遍历

对字符串进行分割,然后循环遍历,每次判断三个单词,看前两个是不是first和second,如果满足的话那么把第三个放到结果中即可。

时间复杂度是O(N)。

Python代码如下:

class Solution(object):
def findOcurrences(self, text, first, second):
"""
:type text: str
:type first: str
:type second: str
:rtype: List[str]
"""
words = text.split()
res = []
L = len(words)
for i in range(L - 2):
f, s, t = words[i], words[i + 1], words[i + 2]
if f == first and s == second:
res.append(t)
return res

C++代码的难点是字符串操作。C++竟然没有split()函数,这也是大家吐槽太多的点。一个可以替换的方案是使用stringstream类,该类会模拟字符串读入操作,即实现字符串按空格分割。初始化三个字符串,分别是f,s,t,读入到第三个里,前两个保留的是前两次读入的结果。这样进行判断是比较优雅的操作。

C++代码如下:

class Solution {
public:
vector<string> findOcurrences(string text, string first, string second) {
stringstream ss(text);
vector<string> res;
string f, s, t;
while (ss >> t) {
if (f == first && s == second)
res.push_back(t);
f = s;
s = t;
}
return res;
}
};

参考资料:
https://leetcode.com/problems/occurrences-after-bigram/discuss/308385/C%2B%2B-stringstream

日期

2019 年 6 月 9 日 —— 简单的题没有难度,需要挑战有难度的才行

【LeetCode】5083. Occurrences After Bigram 解题报告(Python & C++)的更多相关文章

  1. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. R语言与医学统计图形-【25】ggplot图形分面

    ggplot2绘图系统--图形分面 ggplot2的分面faceting,主要有三个函数: facet_grid facet_wrap facet_null (不分面) 1. facet_grid函数 ...

  2. 半天做完的数据报表,YonBuilder只要十几分钟,0代码开发

    进入数字化时代,拍脑袋的决策方式显然不靠谱,一切要靠数据说话.与信息化时代相比,数字化时代的企业对数据的应用更广泛.更深入.为了应对激烈的市场竞争,企业经营决策者们对数据的依赖度越来越高,企业各个业务 ...

  3. C++ 中的多重继承的问题

    如何正确使用C++多重继承 BY R12F · PUBLISHED 2011年06月17日 · UPDATED 2012年03月11日   原创文章,转载请注明:转载自Soul Apogee本文链接地 ...

  4. Ecshop 后台导出订单Excel时, 内存溢出的解决方法

    今天继续跟大家分享一下,在我配置Ecshop时的问题. 今天的问题是在后台想要导出订单列表Excel时出现的内存溢出.错误提示如下 问题:  Fatal error: Allowed memory s ...

  5. 接口测试 python+PyCharm 环境搭建

    1.配置Python环境变量 a:我的电脑->属性->高级系统设置->环境变量->系统变量中的PATH变量. 变量名:PATH      修改变量值为:;C:\Python27 ...

  6. HUD总结

    HUD 指示器/HUD/遮盖/蒙板 半透明的指示器如何实现 指示器的alpha = 1.0; 指示器的背景色是半透明的 1. 创建颜色 直接创建对应的颜色 + (UIColor *)blackColo ...

  7. 如何用shell脚本分析网站日志统计PV、404、500等数据

    以下shell脚本能统计出网站的总访问量,以及404,500出现的次数.统计出来后,可以结合监控宝来进行记录,进而可以看出网站访问量是否异常,是否存在攻击.还可以根据查看500出现的次数,进而判断网站 ...

  8. Shell脚本定期清空大于1G的日志文件

    一个关于如何在指定文件大于1GB后,自动删除的问题. 批处理代码如下: #!/bin/bash # 当/var/log/syslog大于1GB时 # 自动将其备份,并清空 # 注意这里awk的使用 i ...

  9. ES6——>let,箭头函数,this指向小记

    let let允许你声明一个作用域被限制在块级中的变量.语句或者表达式. 还是那个经典的问题:创建5个li,点击不同的li能够打印出当前li的序号. 如果在for循环中使用**var**来声明变量i的 ...

  10. 爬虫之正则表达式re模块

    为什么要学正则表达式 实际上爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的 ...